这个是发送post请求的示例代码,参数是url和json字符串publicstaticStringdoPost(StringapiUrl,Objectjson){CloseableHttpClienthttpClient=HttpClients.createDefault();StringhttpStr=null;HttpPosthttpPost=newHttpPost(apiUrl);CloseableHttpResponseresponse=null;try{httpPost.setConfig(requestConfig);StringEntitystringEntity=newStringEntity(json.toString(),"UTF-8");//解决中文乱码问题stringEntity.setContentEncoding("UTF-8");stringEntity.setContentType("application/json");httpPost.setEntity(stringEntity);response=httpClient.execute(httpPost);HttpEntityentity=response.getEntity();System.out.println(response.getStatusLine().getStatusCode());httpStr=EntityUtils.toString(entity,"UTF-8");}catch(IOExceptione){e.printStackTrace();}finally{if(response!=null){try{EntityUtils.consume(response.getEntity());}catch(IOExceptione){e.printStackTrace();}}}returnhttpStr;}
你用的get吗·参数不是这样直接拼接的吧是用的get啊get不是这样拼接吗?需要把paramMap后面的json字符串进行url编码编码了之后接收方就接不到参数了。。
改一下你请求的ua,改为正常一点的ua,比如:
User-Agent:
Mozilla/5.0(Macintosh;IntelMacOSX10_10_5)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.116Safari/537.36
我觉得你应该改成POST
然后提交数据为:
{"userName":"%E8%B4%BA%E8%BE%BE","card":"430211199606040017","password":"123456","address":"%E6%B9%96%E5%8D%97%E7%9C%81"}
URL为:
http://113.240.245.91:8080/gxxtwhservice/oauth2-server/oauth2/userRegister?access_token=a84776ed393589e1d9e7833e7493b558
这样我觉得比较合理,我还没见过GET时,带了一堆JSON的。这样有点搓。。
post请求加参数的时候如何转成json格式把参数能给下post请求的案例吗谢谢
/**使用方法:JSONObjectparam=newJSONObject();json.put("a",1);...Stringe=HttpClient.sendByPost(this.portUrl,param.toString(),30000);*/packagecom.suning.framework.uaa.util;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.Proxy;importjava.net.URL;importjava.net.URLConnection;importjava.util.logging.Level;importjava.util.logging.Logger;/***建立Http请求操作的封装类**@authorL.J.W*/publicclassHttpClient{privatestaticfinalLoggerLOGGER=Logger.getLogger(HttpClient.class.getName());publicstaticfinalStringCONTENT_TYPE_FORM="application/x-www-form-urlencoded";privatestaticfinalStringHTTP_METHOD_POST="POST";privatestaticfinalStringHTTP_HEADER_CONTENT_TYPE="Content-Type";privatestaticfinalintHTTP_RESPONSE_CODE_FLAG=300;/***通过POST请求方式发送<br>**@paramurl请求URL*@paramparam请求参数*@paramtimeout超时时间*@return发送成功与否*@throwsIOException*/publicstaticStringsendByPost(Stringurl,Stringparam,inttimeout)throwsIOException{URLConnectionconnection=newURL(url).openConnection(Proxy.NO_PROXY);HttpURLConnectioncon=(HttpURLConnection)connection;prepareConnection(con,timeout,HTTP_METHOD_POST);con.getOutputStream().write(param.getBytes());con.getOutputStream().flush();con.getOutputStream().close();validateResponse(con);InputStreamresponseBody=readResponseBody(con);returnreadResult(responseBody);}/***设置HTTP连接相关参数<br>**@paramconnectionhttp连接对象*@paramtimeout超时时间*@parammethod请求方式*@throwsIOException*/privatestaticvoidprepareConnection(HttpURLConnectionconnection,inttimeout,Stringmethod)throwsIOException{if(timeout>=0){connection.setConnectTimeout(timeout);}if(timeout>=0){connection.setReadTimeout(timeout);}connection.setDoOutput(true);connection.setRequestMethod(method);connection.setDoInput(true);connection.setUseCaches(false);connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE,CONTENT_TYPE_FORM);}/***验证响应结果<br>**@paramconHTTP连接对象*@throwsIOException*/privatestaticvoidvalidateResponse(HttpURLConnectioncon)throwsIOException{if(con.getResponseCode()>=HTTP_RESPONSE_CODE_FLAG){try{InputStreames=con.getErrorStream();if(es!=null){LOGGER.log(Level.WARNING,"DidnotreceivesuccessfulHTTPresponse,"+"responsecontentis"+readResult(es));}}catch(Exceptionex){LOGGER.log(Level.WARNING,"ExceptionoccurwhenprocesserrorStream",ex);}thrownewIOException("DidnotreceivesuccessfulHTTPresponse:statuscode="+con.getResponseCode()+",statusmessage=["+con.getResponseMessage()+"]");}}/***读取响应内容<br>**@paramconHTTP连接对象*@return响应内容输入流*@throwsIOException*/privatestaticInputStreamreadResponseBody(HttpURLConnectioncon)throwsIOException{//Plainresponsefound.returncon.getInputStream();}/***解析响应内容<br>**@paramis响应内容输入流*@return响应内容(true/false)*@throwsIOException*/privatestaticStringreadResult(InputStreamis)throwsIOException{BufferedReaderreader=newBufferedReader(newInputStreamReader(is));try{StringBuildertemp=newStringBuilder();Stringline=reader.readLine();while(line!=null&&line!=""){temp.append(line);line=reader.readLine();}returntemp.toString();}finally{reader.close();}}
}
希望被采纳哦。
你后面的参数转下码应该就好了.
止于其他人说post的都是瞎扯淡的.
回复
@szwx855:不知你哪看出来是大数据量了.只能说你用过的第三方接口太少了.你大数据量的json都是转码然后通过get给服务端?在逗我么?