貌似很多人不知道服务器端代码怎么写,在此mark一下: 客户端: ContentType contentType = ContentType.create( HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8); MultipartEntity entity = new MultipartEntity(); ContentBody fileBody = new FileBody(file); entity.addPart("file", fileBody); ContentBody signBody = new StringBody("dolemifasolaxi",contentType); entity.addPart("SIGN", signBody); ContentBody imeiBody = new StringBody("xilasofamiledo",contentType); entity.addPart("IMEI", imeiBody); 服务器代码: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file"); String imei = multipartRequest.getParameter("IMEI"); System.out.println("imei:" + imei); String sign = multipartRequest.getParameter("SIGN"); System.out.println("sign:" + sign);
HttpClient get调用方式
package com.jshx.http.utils; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; public class Test { private void test(){ HttpClient client = new HttpClient(); String queryString = "{\"className\":\"QueryLaw\",\"userId\":\"5\",\"search\":\"6auY5by6\",\"pageSize\":\"10\",\"pageNum\":\"1\"}"; String url = "http://localhost:8080/jsp/http/json_command.action?strTemp="; GetMethod get=new GetMethod(url+queryString); System.out.println("para="+queryString); String result = ""; try { client.executeMethod(get); result = new String(get.getResponseBody(),"UTF-8"); System.out.println("restConn-----------------------------------"+result); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Test test = new Test(); test.test(); } }
HttpClient post调用方式
package com.jshx.http.utils; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class postTest { private void test(){ HttpClient client = new HttpClient(); String queryString = "{\"userId\":\"20130705144110|18963603541\",\"phoneName\":\"华为190\",\"phoneVersion\":\"v.1.1\",\"className\":\"SaveQuick\",\"type\":\"1\",\"queryMonth\":\"2013-02\",\"startTime\":\"2013-02-03 11:16\",\"endTime\":\"2013-02-03 14:16\"}"; PostMethod get = new PostMethod("http://localhost:8080/servlet/JsonServlet"); NameValuePair[] data = new NameValuePair[1]; NameValuePair nameValuePair1 = new NameValuePair("jsonStr", queryString); //NameValuePair nameValuePair2 = new NameValuePair("docId", "42"); data[0]=nameValuePair1; //data[1]=nameValuePair2; get.setRequestBody(data); System.out.println("para="+queryString); String result = ""; try{ client.executeMethod(get); //byte[] responseBody = get.getResponseBody(); StringBuffer sb = new StringBuffer(); sb.append(get.getResponseBody()); result = new String(get.getResponseBody(), "utf-8"); System.out.println("result="+result); }catch (Exception e) { // TODO: handle exception } } public static void main(String[] args) { postTest pt = new postTest(); pt.test(); } }
相关推荐
测试httpclient,主要测试json参数的接口,包括get请求和post请求。
支持get、post等web请求,非常适合后台接口的调试、测试。
本方法以HttpClient发送请求,并且接收返回数据 举例说明 public static String doGet(String url, Map, String> params, String charset) public static void downLoadImage(String url, String path) public ...
基于jmeter+Java+HttpclientUtil实现的接口测试工具,通过Excel表格进行维护接口相关参数信息,借助Jmeter工具通过java请求设计接口测试自动化测试用例。代码重写了JavaSampleClient类,在使用过程中取出了冗余的...
基已HttpClient模拟发送请求,文件上传测试用例。
本人的测试用,包括压缩及解压缩,完整模拟,处女作,不想做伸手党
使用本机开启服务,并利用此程序联机到Server,联机完成后,可发送请求,并回收结果,同时将收到的讯息转换为JSON序列
以上自己测试是可行的,直接拿过去导入即可,直接用main
httpclient-4.3.4.jar 和 httpcore-4.3.2 打包下载,测试可用。请放心使用 httpclient-4.3.4.jar 和 httpcore-4.3.2 打包下载,测试可用。请放心使用
* 异步方法的Fluent写法 */ public void testAsyncHttpFluent() { SimpleRequest.Get("http://www.baidu.com") .header("h1", "hv1") .header("h2", "hv2") .parameter("p1", "pv1") .parameter("p2", "pv2...
2.创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。 //使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的 http换成https ...
httpClient自动接受https请求的所有证书代码,修改相应测试url,主函数运行即可
JAVA调用HttpClient进行HTTP请求,亲测通过,内含测试程序(已测试get和post成功)。
:check_mark_button: 可以提供通配符(即我不在乎请求端点或请求HTTP方法等) :check_mark_button: 可以提供多个端点,并查看处理基于特定请求返回的内容。 :check_mark_button: 可以确认尝试击中端点的次数。 :...
内容概要:Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口...
httpclient-4.5.4.jar包,和一些工具类,用于测试HttpClient服务端跨域请求,已经浏览器跨域问题采用JSONP技术,博客地址http://blog.csdn.net/zhaogangyyxf/article/details/79550609
基于HttpClientUtils工具类进行Http/Https协议请求封装
花了大半天写的测试类,用的时候记得改下引用的包名,可以当util工具类用,其他说明见博客 https://blog.csdn.net/akxj2022/article/details/88691698 流方式发送接收: ...觉得可以的话,又不想复制粘贴,就花点积分...
HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送HTTP请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。