httpclient调用代码案例

 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
 

/**
 *
 *
 */
public class HttpUtil {

 //post请求,jsonobj为请求体,json转为String的格式
 public static String httpPost(String url, String jsonObj) {
   HttpPost post = null;
   String json = "";
      try {
          HttpClient httpClient = new DefaultHttpClient();
          post = new HttpPost(url);
          // 构造消息头
          post.setHeader("Content-type", "application/json;charset=UTF-8");
          post.setHeader("Connection", "Close");
          // 构建消息实体
          StringEntity entity = new StringEntity(jsonObj.toString(),"UTF-8");
          entity.setContentEncoding("UTF-8");
          // 发送Json格式的数据请求
          entity.setContentType("application/json");
          post.setEntity(entity);
          HttpResponse response = httpClient.execute(post);
          if (response.getStatusLine().getStatusCode() != 200) {
     throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }
    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()),"UTF-8"));
    StringBuilder output = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
     output.append(line);
    }
    httpClient.getConnectionManager().shutdown();
    json = output.toString();
    Map<String,Object> jsonMap = GsonUtils.toLinkedHashMap(json, Object.class);
    System.out.println(jsonMap);
    if(jsonMap.get("data") != null) {
     json = GsonUtils.ObjectToString(jsonMap.get("data"));
    }else {
     json = GsonUtils.ObjectToString(jsonMap);
    }
    System.out.println(json);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return json;
 }
 public static String httpGet(String url,String jsonObj) {
   HttpGet get = null;
   String json = "";
      try {
          HttpClient httpClient = new DefaultHttpClient();
          get = new HttpGet(url);
          // 构造消息头
         get.setHeader("Content-type", "application/json;charset=UTF-8");
          get.setHeader("Connection", "Close");
          HttpResponse response = httpClient.execute(get);
             
          if (response.getStatusLine().getStatusCode() != 200) {
     throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }
    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()),"UTF-8"));
    StringBuilder output = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
     output.append(line);
    }
    httpClient.getConnectionManager().shutdown();
    json = output.toString();
    Map<String,Object> jsonMap = GsonUtils.toLinkedHashMap(json, Object.class);
    System.out.println(jsonMap);
    if(jsonMap.get("data") != null) {
     json = GsonUtils.ObjectToString(jsonMap.get("data"));
    }else {
     json = GsonUtils.ObjectToString(jsonMap);
    }
    System.out.println(json);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return json;
 }
}

posted on 2020-04-22 14:25  ran_D  阅读(300)  评论(0)    收藏  举报

导航