POST, GET call an external server interface to obtain returns Json data

//1. POST调用外部服务器接口,获取返回Json数据
private String getClientData(Map<String, String> params, String url) throws AppException {
  URL connect;
  StringBuffer data = new StringBuffer();
  try {
    //创建连接
    connect = new URL(url);
    HttpURLConnection connection = (HttpURLConnection)connect.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setUseCaches(false);//post不能使用缓存
    connection.setInstanceFollowRedirects(true);
    connection.setRequestProperty("accept", "*/*");
    connection.setRequestProperty("connection", "Keep-Alive");
    connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    OutputStreamWriter paramout = new OutputStreamWriter(
    connection.getOutputStream(),"UTF-8");

    //拼接Post 请求的参数
    String paramsStr = "";
    for(String param : params.keySet()){
      paramsStr += "&" + param + "=" + params.get(param);
    }
    if(!paramsStr.isEmpty()){
      paramsStr = paramsStr.substring(1);
    }
    paramout.write(paramsStr);
    paramout.flush();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    String line;
    the while ((Line = reader.readLine ()) = null!) {
      // return the interface data
      data.append (Line);
    }

    paramout.close ();
    reader.Close ();
  } the catch (Exception E) {
    E .printStackTrace ();
  return null;
  }
  return data.toString ();
}
// 2 calls the GET interface to an external server, obtain data returned Json.
@SuppressWarnings ({ "Resource"})
Private String the getData (the Map <String, String> the params, String URL) {
  String Result = "";
  // splicing the URL
  String paramsStr = "";
  List <String> = new new List the ArrayList <String> (params.keySet ());
  for (int I = 0; I < list.size (); I ++) {
    IF (I == 0) {
      paramsStr = "?" + list.get(0) + "=" + params.get(list.get(0));
    } else {
      paramsStr += "&" + list.get(i) + "=" + params.get(list.get(i));
    }
  }
  try {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url + paramsStr);
    get.setHeader("charset", "UTF-8");

    org.apache.http.HttpResponse response = client.execute(get);
    if (200 == response.getStatusLine().getStatusCode()) {
      result = EntityUtils.toString(response.getEntity(), "UTF-8");
    }
  } catch (ClientProtocolException e) {
    e.printStackTrace();
    return null;
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  }
  return result;
}

//3. 附录:引包
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freework.Constant;
import com.freework.base.exception.AppException;
import com.freework.freedbm.bean.Criteria;
import com.freework.freedbm.util.PageGridParam;
import com.freework.freedbm.util.PageTotalResult;

Guess you like

Origin www.cnblogs.com/sunshijia1993/p/11318941.html