Java模拟POST表单提交HttpClient操作
2016-01-26 10:07
701 查看
public static void Login() {
String url = "http://www.***.com/login";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = {
new NameValuePair("account", "yijianfeng_vip@163.com"),
new NameValuePair("nextUrl", ""),
new NameValuePair("lcallback", ""),
new NameValuePair("password ", "******"),
new NameValuePair("persistent", "1"), };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("diandianLogin:" + location);
} else {
System.err.println("Location field value is null.");
}
return;
} else {
System.out.println(postMethod.getStatusLine());
String str = "";
try {
str = postMethod.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(str);
}
postMethod.releaseConnection();
return;
}
其中需要的jar包:
1、 commons-httpclient.jar
2、commons-codec.jar
3、commons-logging.jar
今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文乱码问题。
请求端代码:
Java代码
![](http://sunyf.iteye.com/images/icon_star.png)
/**
* HttpClient提交参数
* @author sunyunfang@126.com
*/
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");
// 使用POST方式提交数据
HttpMethod method = getPostMethod();
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}
// 使用POST方式提交数据
private static HttpMethod getPostMethod() {
String url = "/PushServer/notification.do?action=sendOneMsg";
NameValuePair message = new NameValuePair("message", "消息内容。");
post.setRequestBody(new NameValuePair[]{message});
return post;
}
// 使用GET方式提交数据
private static HttpMethod getGetMethod() {
return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");
}
目标端代码:
Java代码
![](http://sunyf.iteye.com/images/icon_star.png)
/**
* 供MsgServer远程调用
* @param request
* @param response
* @return
* @throws Exception
* @author SunYunfang@126.com
*/
public ModelAndView sendOneMsg(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String message = ServletRequestUtils.getStringParameter(request, "message");
}
这段代码执行后,目标能收到信息,但是中文乱码,也没有找到转码的方法。
经分析,原来使用 NameValuePair 加入的HTTP请求的参数最终都会转化为 RequestEntity
提交到HTTP服务器。接着在PostMethod的父类 EntityEnclosingMethod
中发现,只要重载getRequestCharSet()方法就能设置提交的编码(字符集)。
修正后:
Java代码
![](http://sunyf.iteye.com/images/icon_star.png)
/**
* HttpClient提交参数
* @author SunYunfang@126.com
*/
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");
// 使用POST方式提交数据
HttpMethod method = getPostMethod();
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}
// 使用POST方式提交数据
private HttpMethod getPostMethod() {
String url = "/PushServer/notification.do?action=sendOneMsg";
PostMethod post = new UTF8PostMethod(url);
NameValuePair message = new NameValuePair("message", "消息内容。");
post.setRequestBody(new NameValuePair[]{message});
return post;
}
//Inner class for UTF-8 support
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
// 使用GET方式提交数据
private static HttpMethod getGetMethod() {
return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");
}
String url = "http://www.***.com/login";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = {
new NameValuePair("account", "yijianfeng_vip@163.com"),
new NameValuePair("nextUrl", ""),
new NameValuePair("lcallback", ""),
new NameValuePair("password ", "******"),
new NameValuePair("persistent", "1"), };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("diandianLogin:" + location);
} else {
System.err.println("Location field value is null.");
}
return;
} else {
System.out.println(postMethod.getStatusLine());
String str = "";
try {
str = postMethod.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(str);
}
postMethod.releaseConnection();
return;
}
其中需要的jar包:
1、 commons-httpclient.jar
2、commons-codec.jar
3、commons-logging.jar
今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文乱码问题。
请求端代码:
Java代码
![](http://sunyf.iteye.com/images/icon_star.png)
/**
* HttpClient提交参数
* @author sunyunfang@126.com
*/
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");
// 使用POST方式提交数据
HttpMethod method = getPostMethod();
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}
// 使用POST方式提交数据
private static HttpMethod getPostMethod() {
String url = "/PushServer/notification.do?action=sendOneMsg";
NameValuePair message = new NameValuePair("message", "消息内容。");
post.setRequestBody(new NameValuePair[]{message});
return post;
}
// 使用GET方式提交数据
private static HttpMethod getGetMethod() {
return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");
}
目标端代码:
Java代码
![](http://sunyf.iteye.com/images/icon_star.png)
/**
* 供MsgServer远程调用
* @param request
* @param response
* @return
* @throws Exception
* @author SunYunfang@126.com
*/
public ModelAndView sendOneMsg(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String message = ServletRequestUtils.getStringParameter(request, "message");
}
这段代码执行后,目标能收到信息,但是中文乱码,也没有找到转码的方法。
经分析,原来使用 NameValuePair 加入的HTTP请求的参数最终都会转化为 RequestEntity
提交到HTTP服务器。接着在PostMethod的父类 EntityEnclosingMethod
中发现,只要重载getRequestCharSet()方法就能设置提交的编码(字符集)。
修正后:
Java代码
![](http://sunyf.iteye.com/images/icon_star.png)
/**
* HttpClient提交参数
* @author SunYunfang@126.com
*/
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");
// 使用POST方式提交数据
HttpMethod method = getPostMethod();
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}
// 使用POST方式提交数据
private HttpMethod getPostMethod() {
String url = "/PushServer/notification.do?action=sendOneMsg";
PostMethod post = new UTF8PostMethod(url);
NameValuePair message = new NameValuePair("message", "消息内容。");
post.setRequestBody(new NameValuePair[]{message});
return post;
}
//Inner class for UTF-8 support
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
// 使用GET方式提交数据
private static HttpMethod getGetMethod() {
return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");
}
相关文章推荐
- iOS 从网络获取json数据的系统方法
- ajax--返回值类型response
- 图解TCP-IP协议
- Android程序开发ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)
- SDWebImage提取https图片失败
- HTTPS站点搭建教程:Win7/Windows Server 2008R2
- 【JAVA】Spring HTTP Invoker 远程服务调用
- java模拟从http上下载文件
- 网络丢包严重的解决办法
- 网络流(dinic)
- 升级 TCP 协议将使网速提升30%,中国受益尤为明显
- 运维小知识之nginx---..nginx-sticky-module-1.1ngx_http_sticky_misc.cIn function ‘ngx_http_sticky_misc_text_
- AsyncTask异步任务学习笔记(一、异步任务加载网络图片(多图))
- AngualrJS中每次$http请求时的一个遮罩层Directive
- Android 发送http请求实例
- LAMP环境由http转到https
- 解决android 6.0(api 23) SDK,不再提供org.apache.http.*
- TCP UDP HTTP
- 网络字节序和本地字节序的理解和实现
- SIM808 执行HTTP POST 例程【全网首发】