POST、GET调用外部服务器接口,获取返回Json数据

//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;
    while ((line = reader.readLine()) != null) {
      //接口返回的数据
      data.append(line);
    }

    paramout.close();
    reader.close();
  } catch (Exception e) {
    e.printStackTrace();
  return null;
  }
  return data.toString();
}
//2. GET调用外部服务器接口,获取返回Json数据
@SuppressWarnings({ "resource" })
private String getData(Map<String, String> params, String url) {
  String result = "";
  // 拼接URL
  String paramsStr = "";
  List<String> list = new 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;

猜你喜欢

转载自www.cnblogs.com/sunshijia1993/p/11318941.html