博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
发送HTTP请求 -- HttpUtil
阅读量:5732 次
发布时间:2019-06-18

本文共 5029 字,大约阅读时间需要 16 分钟。

1.

1 package com.step.utils;  2   3 import java.io.IOException;  4 import java.net.URLDecoder;  5 import java.util.ArrayList;  6 import java.util.List;  7   8 import org.apache.http.HttpResponse;  9 import org.apache.http.HttpStatus; 10 import org.apache.http.NameValuePair; 11 import org.apache.http.client.ClientProtocolException; 12 import org.apache.http.client.HttpClient; 13 import org.apache.http.client.entity.UrlEncodedFormEntity; 14 import org.apache.http.client.methods.HttpGet; 15 import org.apache.http.client.methods.HttpPost; 16 import org.apache.http.entity.StringEntity; 17 import org.apache.http.impl.client.DefaultHttpClient; 18 import org.apache.http.message.BasicNameValuePair; 19 import org.apache.http.util.EntityUtils; 20  21 import com.alibaba.fastjson.JSONObject; 22  23  24  25 /** 26  * 发送HTTP请求 27  * @author mlu 28  * 29  */ 30 public class HttpUtils { 31      32     /** 33      * 发送post请求--用于接口接收的参数为JSON字符串 34      * @param url 请求地址 35      * @param params json格式的字符串 36      * @return 37      */ 38     public static String httpPost(String url, String params){ 39         String result = null; 40         try { 41             HttpClient httpClient = new DefaultHttpClient(); 42             HttpPost httpPost = new HttpPost(url); 43             /* 44              * 发送json字符串,这两句需要设置 45              */ 46             httpPost.addHeader("Content-type","application/json; charset=utf-8");   47             httpPost.setHeader("Accept", "application/json");   48              49             httpPost.setEntity(new StringEntity(params, "UTF-8")); 50              51             HttpResponse response = httpClient.execute(httpPost);   52              53             int statusCode = response.getStatusLine().getStatusCode();   54                55             if (statusCode == HttpStatus.SC_OK) {   56                 // Read the response body   57                 result = EntityUtils.toString(response.getEntity(),"UTF-8");   58             }  59         } catch (Exception e) { 60             e.printStackTrace(); 61         } 62         return result; 63     } 64      65     /** 66      * 发送post请求--用于接口接收的参数为键值对 67      * @param url 请求地址 68      * @param nameValuePairs 键值对 69      * @return 70      */ 71     public static String httpPost(String url, List
nameValuePairs) { 72 HttpClient httpClient = new DefaultHttpClient(); 73 HttpPost httpPost = new HttpPost(url); 74 String strResult = ""; 75 76 try { 77 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 78 HttpResponse response = httpClient.execute(httpPost); 79 80 if (response.getStatusLine().getStatusCode() == 200) { 81 /* 读返回数据 */ 82 strResult = EntityUtils.toString(response.getEntity()); 83 // System.out.println("conResult:"+conResult); 84 } else { 85 strResult += "发送失败:" + response.getStatusLine().getStatusCode(); 86 } 87 } catch (ClientProtocolException e) { 88 e.printStackTrace(); 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } 92 93 return strResult; 94 } 95 96 public static String httpGet(String url, List
nameValuePairs){ 97 HttpClient httpClient = new DefaultHttpClient(); 98 String sb = ""; 99 String result = "";100 try {101 for(NameValuePair nvp:nameValuePairs){102 sb += nvp.getName()+"="+nvp.getValue()+"&";103 }104 sb = sb.substring(0,sb.length()-1);105 sb = URLDecoder.decode(sb, "UTF-8");106 HttpGet httpGet = new HttpGet(url+"?"+sb);107 108 HttpResponse response = httpClient.execute(httpGet);109 if (response.getStatusLine().getStatusCode() == 200) {110 /* 读返回数据 */111 result = EntityUtils.toString(response.getEntity());112 } else {113 result += "发送失败:" + response.getStatusLine().getStatusCode();114 }115 116 } catch (ClientProtocolException e) {117 e.printStackTrace();118 } catch (IOException e) {119 e.printStackTrace();120 }121 return result;122 }123 124 public static void main(String[] args) {125 String url = "http://10.140.8.56/gd_fssc/rest/fsscService/getStaffInfo";126 String url2 = "http://localhost:8080/eshore-app-backframe-web/interface/getJson";127 128 // 发送 POST 请求129 JSONObject json = new JSONObject();130 json.put("number", "44053211@GD");131 httpPost(url,json.toString());132 133 List
nameValuePairs = new ArrayList<>();134 nameValuePairs.add(new BasicNameValuePair("method", "login"));135 httpGet(url2,nameValuePairs);136 }137 }

引用的jar包:

转载于:https://www.cnblogs.com/sharpest/p/7789208.html

你可能感兴趣的文章
python分类
查看>>
linux 中常见的压缩和解压缩的命令
查看>>
GitBlit (1)-- 在linux 安装 GitBlit 并运行
查看>>
Windows与Linux之间的文件自动同步
查看>>
topcoder srm 714 div1
查看>>
20160215
查看>>
mxnet导入图像数据
查看>>
程序是如何执行的(一)a=a+1
查看>>
go : 结构
查看>>
【Python第五篇】Python面向对象(初级篇)
查看>>
innobackupex参数之 --throttle 限速这个值设置多少合理 原创
查看>>
18 已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果
查看>>
BZOJ - 3578: GTY的人类基因组计划2
查看>>
理解WebKit和Chromium(电子书)
查看>>
爱——无题
查看>>
分布式服务框架原来与实践 读书笔记一
查看>>
Aho-Corasick automation-KMP
查看>>
【http】post和get请求的区别
查看>>
/etc/profile
查看>>
摘记总结(1)
查看>>