码迷,mamicode.com
首页 > 编程语言 > 详细

Java利用Http模拟表单提交

时间:2015-08-27 13:34:15      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:java   http   表单提交   

private static String sendPost(String url, NameValuePair[] params) {
HttpClient client = new HttpClient();
// 请求超时
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
// 读取超时
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
// 模拟登录页面
PostMethod post = new PostMethod(url);
post.setRequestBody(params);
if (cookies != null && cookies.length > 0) {
client.getState().addCookies(cookies);
}
try {
client.executeMethod(post);
} catch (Exception e) {
e.printStackTrace();
}
if (post.getStatusCode() == 200) {
StringBuffer sbf = new StringBuffer();
BufferedReader reader = null;
try {
InputStream inputStream = post.getResponseBodyAsStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = reader.readLine()) != null) {
sbf.append(line);
}
// 查看 cookie 信息
cookies = client.getState().getCookies();
return sbf.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
post.releaseConnection();
return null;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java利用Http模拟表单提交

标签:java   http   表单提交   

原文地址:http://blog.csdn.net/yj_vs_gy/article/details/48024255

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!