Java uses httpclient to request the general mvc interface

 

Java uses httpclient to request the general mvc interface: there is no cross-domain problem, and it is not as complicated as webservrice

Ajxa can also call mvc type interfaces, but there are cross-domain problems, which can be handled with interceptors:

 

 

Tools:

 

package com.itm.weixin.common;

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

public class HttpClient {

 

private static final Log logger = LogFactory.getLog(HttpClient.class);

 

public static String sendGet(String url, String param) throws Exception {

        String result = "";

        HttpURLConnection httpConn = null;

        BufferedReader in = null;

        try {

            String urlNameString = url + "?" + param;

            URL realUrl = new URL(urlNameString);

            // open the connection to the URL

            httpConn = (HttpURLConnection) realUrl.openConnection();

            // Set common request attributes

            //httpConn.setRequestProperty("accept", "*/*");

            httpConn.setRequestProperty("Accept", "application/json");

            httpConn.setRequestProperty("Accept-Charset", "UTF-8");

            httpConn.setConnectTimeout(5000); 

            httpConn.setReadTimeout(5000); 

            httpConn.setDoOutput(true);

            // establish the actual connection

            httpConn.connect();

            int responseCode = httpConn.getResponseCode();

            if (responseCode != 200) {

            logger.error("Http request Error,responseCode=" + responseCode +", requestUrl:" + url +",requestParamsJson=" + param);

            throw new Exception("Http request Error,responseCode=" + responseCode + ", requestUrl:" + url + ",requestParamsJson=" + param); 

            } else {

            logger.info("get Success!" + "requestUrl:" + url + ", requestParamsJson:" + param);

            }

            // Define the BufferedReader input stream to read the response from the URL

            in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));

            String line;

            while ((line = in.readLine()) != null) {

                result += line;

            }

        } catch (Exception e) {

        throw e;

        } finally {

        httpConn.disconnect();

            try {

                if (in != null) {

                    in.close();

                }

            } catch (Exception e1) {

            throw e1;

            }

        }

        return result;

    }

 

}

 

 

 

 

application:

 

reqUrl = ConfigInfo.getPropertiesValue("betweenAllowedRangeUrl");

WeixinApplyInfo weixinApplyInfo = (WeixinApplyInfo) baseService.findObject(WeixinApplyInfo.class, "orderNo", orderNoReq);

reqParam = "productType=" + saleFollowEntity.getProductCode() +"&amount=" + weixinApplyInfo.getApplyAmount();

 

 

resultJson = HttpClient.sendGet(reqUrl, reqParam);

 

JSONObject applyAmountJsonObject = JSONObject.fromObject(resultJson);

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326232550&siteId=291194637