import java.io.IOException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * */ public class HttpClientUtil { public static void main(String arg[]) throws Exception { String url = "test.com"; JSONObject params = new JSONObject(); params.put("SRC_STM_CODE", "wsf"); params.put("TENANT_ID", "123"); params.put("NM", "张三"); params.put("BRTH_DT", "1983-01-20"); params.put("GND_CODE", "1"); JSONArray params2 = new JSONArray(); JSONObject param3 = new JSONObject(); param3.put("DOC_TP_CODE", "10100"); param3.put("DOC_NBR", "100200198301202210"); param3.put("DOC_CUST_NM", "test"); params2.add(param3); params.put("DOCS", params2); String ret = doPost(url, params).toString(); System.out.println(ret); } /** httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。 * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get * 方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。 */ /* 1 生成 HttpClinet 对象并设置参数 */ HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod getMethod = new GetMethod(url); // 设置 get 请求超时为 5 秒 getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000); // 设置请求重试处理,用的是默认的重试处理:请求三次 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); String response = ""; /* 3 执行 HTTP GET 请求 */ try { int statusCode = httpClient.executeMethod(getMethod); /* 4 判断访问的状态码 */ if (statusCode != HttpStatus.SC_OK) { System.err.println("请求出错: "+ getMethod.getStatusLine()); } /* 5 处理 HTTP 响应内容 */ // HTTP响应头部信息,这里简单打印 Header[] headers = getMethod.getResponseHeaders(); for (Header h : headers) System.out.println(h.getName() + "------------ " + h.getValue()); // 读取 HTTP 响应内容,这里简单打印网页内容 byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组 response = new String(responseBody, charset); System.out.println("----------response:" + response); // 读取为 InputStream,在网页内容数据量大时候推荐使用 // InputStream response = getMethod.getResponseBodyAsStream(); } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有问题 System.out.println("请检查输入的URL!"); e.printStackTrace(); } catch (IOException e) { // 发生网络异常 System.out.println("发生网络异常!"); e.printStackTrace(); } finally { /* 6 .释放连接 */ getMethod.releaseConnection(); } return response; } /** * post请求 * @param url * @param json * @return */ public static JSONObject doPost(String url,JSONObject json){ DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); JSONObject response = null; try { StringEntity s = new StringEntity(json.toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/json");//发送json数据需要设置contentType post.setEntity(s); HttpResponse res = client.execute(post); if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(res.getEntity());// 返回json格式: response = JSONObject.fromObject(result); } } catch (Exception e) { throw new RuntimeException(e); } return response; } }
相关推荐
httpclient get/post请求工具类(map参数封装),方便调用
利用c# httpclient ,post json数据到web api
kettle抽取http请求,发送json参数示例
这个代码的作用是使用HttpClient发送POST请求,...通过学习这个示例,你可以了解到如何使用HttpClient发送不同类型的HTTP请求(如GET、POST、PUT、DELETE等),以及如何处理响应结果(如读取响应体、解析JSON数据等)。
Angular中的数据请求 内置模块HttpClient实现(get post jsonp 以及第三方模板axios请求数据(32分12秒)
本方法以HttpClient发送请求,并且接收返回数据 举例说明 public static String doGet(String url, Map, String> params, String charset) public static void downLoadImage(String url, String path) public ...
通过HTTPClient发起HTTPS GET请求(返回值为字符串)、POST请求(返回值为JSONOBJECT),需引入fastJson的jar包
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
VS2017 下Xamarin移动开发Get,Post服务请求源码封装,支持实体对象直接发送Request请求: var result = HttpClientHelper.GetInstance.PostT, GetStoreTVProductInfoResponseDto>("GetStoreTVProductInfo", request...
测试httpclient,主要测试json参数的接口,包括get请求和post请求。
VS2017 下Xamarin移动开发Get,Post服务请求源码封装,支持实体对象直接发送Request请求: var result = HttpClientHelper.GetInstance.PostT, GetStoreTVProductInfoResponseDto>("GetStoreTVProductInfo", request...
httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:...
HttpClient的工具类,用于发送get和post请求的工具类,数据类型可以使XML,JSON
http接口通用调试工具,支持get、post、put、patch、delete方法,数据的发送和接收使用json格式,即Content-Type: application/json
本demo有两个例子test是作为服务端开启的,httpclient是客户端调用的,模拟服务端的test在启动后是没有界面的,要加上id才能看到json数据,httpclient的HttpClient33.java是测试类,最后的结果是接受到一个对象。...
*创建一个请求对象(post/get) *准备参数(构建参数对象) *发送请求 *获取结果 *ajax方式请求到LBS云服务的相关接口 从一个项目通过ajax请求到另一个项目的数据 $.post("../../XX.action",{},function(data){ ...
利用httpclient封装了http的POST和GET请求方式,封装方法多样,返回结果为json
4)对请求的数据根据URL进行缓存到本地sqlite数据库中(我刚刚想起了 post请求的时候需要对缓存url及本地数据存取做修改才可以正常用 如果不想进行修改可以用get形式) 5)本demo可以直接运行 本来想测试protobuf...