httpclient怎么传递json类型的参数

 我来答
匿名用户
2017-07-02
展开全部
importjava.io.IOException;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importorg.apache.commons.httpclient.DefaultHttpMethodRetryHandler;importorg.apache.commons.httpclient.Header;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.HttpStatus;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.httpclient.params.HttpMethodParams;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;/****/publicclassHttpClientUtil{publicstaticvoidmain(Stringarg[])throwsException{Stringurl="test.com";JSONObjectparams=newJSONObject();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");JSONArrayparams2=newJSONArray();JSONObjectparam3=newJSONObject();param3.put("DOC_TP_CODE","10100");param3.put("DOC_NBR","100200198301202210");param3.put("DOC_CUST_NM","test");params2.add(param3);params.put("DOCS",params2);Stringret=doPost(url,params).toString();System.out.println(ret);}/**httpClient的get请求方式2*@return*@throwsException*/publicstaticStringdoGet(Stringurl,Stringcharset)throwsException{/**使用GetMethod来访问一个URL对应的网页,实现步骤:1:生成一个HttpClinet对象并设置相应的参数。*2:生成一个GetMethod对象并设置响应的参数。3:用HttpClinet生成的对象来执行GetMethod生成的Get*方法。4:处理响应状态码。5:若响应正常,处理HTTP响应内容。6:释放连接。*//*1生成HttpClinet对象并设置参数*/HttpClienthttpClient=newHttpClient();//设置Http连接超时为5秒httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);/*2生成GetMethod对象并设置参数*/GetMethodgetMethod=newGetMethod(url);//设置get请求超时为5秒getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);//设置请求重试处理,用的是默认的重试处理:请求三次getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,newDefaultHttpMethodRetryHandler());Stringresponse="";/*3执行HTTPGET请求*/try{intstatusCode=httpClient.executeMethod(getMethod);/*4判断访问的状态码*/if(statusCode!=HttpStatus.SC_OK){System.err.println("请求出错:"+getMethod.getStatusLine());}/*5处理HTTP响应内容*///HTTP响应头部信息,这里简单打印Header[]headers=getMethod.getResponseHeaders();for(Headerh:headers)System.out.println(h.getName()+"------------"+h.getValue());//读取HTTP响应内容,这里简单打印网页内容byte[]responseBody=getMethod.getResponseBody();//读取为字节数组response=newString(responseBody,charset);System.out.println("----------response:"+response);//读取为InputStream,在网页内容数据量大时候推荐使用//InputStreamresponse=getMethod.getResponseBodyAsStream();}catch(HttpExceptione){//发生致命的异常,可能是协议不对或者返回的内容有问题System.out.println("请检查输入的URL!");e.printStackTrace();}catch(IOExceptione){//发生网络异常System.out.println("发生网络异常!");e.printStackTrace();}finally{/*6.释放连接*/getMethod.releaseConnection();}returnresponse;}/***post请求*@paramurl*@paramjson*@return*/publicstaticJSONObjectdoPost(Stringurl,JSONObjectjson){DefaultHttpClientclient=newDefaultHttpClient();HttpPostpost=newHttpPost(url);JSONObjectresponse=null;try{StringEntitys=newStringEntity(json.toString());s.setContentEncoding("UTF-8");s.setContentType("application/json");//发送json数据需要设置contentTypepost.setEntity(s);HttpResponseres=client.execute(post);if(res.getStatusLine().getStatusCode()==HttpStatus.SC_OK){HttpEntityentity=res.getEntity();Stringresult=EntityUtils.toString(res.getEntity());//返回json格式:response=JSONObject.fromObject(result);}}catch(Exceptione){thrownewRuntimeException(e);}returnresponse;}}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式