httpclient怎么传递json类型的参数
答案:1 悬赏:0 手机版
解决时间 2021-02-24 03:44
- 提问者网友:wodetian
- 2021-02-23 04:54
httpclient怎么传递json类型的参数
最佳答案
- 五星知识达人网友:胯下狙击手
- 2021-02-23 05:56
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);}publicstaticStringdoGet(Stringurl,Stringcharset)throwsException{HttpClienthttpClient=newHttpClient();//设置Http连接超时为5秒httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);GetMethodgetMethod=newGetMethod(url);//设置get请求超时为5秒getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);//设置请求重试处理,用的是默认的重试处理:请求三次getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,newDefaultHttpMethodRetryHandler());Stringresponse="";try{intstatusCode=httpClient.executeMethod(getMethod);if(statusCode!=HttpStatus.SC_OK){System.err.println("请求出错:"+getMethod.getStatusLine());}//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{getMethod.releaseConnection();}returnresponse;}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;}}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯