httpclient发送无参数的post数据
public static void main(String[]
args) throws Exception
{
HttpClient client = HttpClients. createDefault();
HttpPost post = new HttpPost("http://127.0.0.1/report/testPost" );
//组装一个 json串,用于发送
JSONObject jsonObj = new JSONObject();
jsonObj.put( "website" , "http://www.dutycode.com" );
StringEntity entity = new StringEntity(jsonObj.toJSONString());
entity.setContentEncoding( "UTF-8" );
entity.setContentType( "application/json" );//设置为 json数据
post.setEntity(entity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
String res = EntityUtils. toString(resEntity);
System. out .println(res);
}
|
@Path ("testPost" )
public ActionResult
getpost() throws Exception{
StringBuilder sb
= new StringBuilder ();
InputStream is = getRequest().getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
byte []
buffer = new byte[1024];
int read
= 0;
while ((read=bis.read(buffer))
!= -1){
sb.append( new String(buffer,
0, read, "UTF-8" ));
}
System. out .println(sb.toString());
return outputStream("{msg:success}" );
}
|
{msg:success}
|
{"email":"[email protected]","website":"http://www.dutycode.com"}
|
版权所有:《攀爬蜗牛》 => 《httpclient发送无参数的post数据》
本文地址:http://www.dutycode.com/post-76.html
除非注明,文章均为 《攀爬蜗牛》 原创,欢迎转载!转载请注明本文地址,谢谢
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。