您的位置:首页 > 理论基础 > 计算机网络

httpclient不带参数获取值

2016-06-20 00:00 369 查看
客户端

public String postFormCom(String url, String params) {
// 创建默认的httpClient实例.
HttpClient httpclient = HttpClientBuilder.create().build();
// 创建httppost
HttpPost httppost = new HttpPost(url);
try {
StringEntity reqEntity = new StringEntity(params, "utf-8");
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getURI());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
String responseStr = EntityUtils.toString(entity, "utf-8");
System.out.println("Response content: " + responseStr);
System.out.println("--------------------------------------");
return responseStr;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httppost.releaseConnection();
}
return null;
}

服务器端

@RequestMapping( { "/test.htm" })
public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream is = request.getInputStream();
System.out.println(convertStreamToString(is));
}

public String convertStreamToString(InputStream is) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line).append("\n");
}
return buffer.toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: