Java模拟POST表单提交HttpClient操作
- 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方式提交数据,目标收到后中文乱码问题。
请求端代码:
- /**
- * 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");
- }
目标端代码:
- /**
- * 供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()方法就能设置提交的编码(字符集)。
修正后:
- /**
- * 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");
- }
Java模拟POST表单提交HttpClient操作的更多相关文章
- java模拟from表单提交,上传图片
/** * java上传表单,有图片 * @param urlStr 上传地址 * @param textMap 表单参数 * @param fileMap 文件参数 key:文件名称 value:文 ...
- 通过HttpURLConnection模拟post表单提交
通过HttpURLConnection模拟post表单提交 package junit; import java.io.InputStream; import java.net.HttpURLConn ...
- js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题
js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...
- js_ajax模拟form表单提交_多文件上传_支持单个删除
需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...
- Linux curl 模拟form表单提交信息和文件
Linux curl 模拟form表单提交信息和文件 curl是一个命令行方式下传输数据的开源传输工具,支持多种协议:FTP.HTTP.HTTPS.IMAP.POP3.TELNET等,功能超级强大 ...
- jquery模拟form表单提交并新打开页面
/** * form表单提交本页面打开 * @param url * @param params */ function postCurrent(url,params){ var form = $(& ...
- Ajax模拟Form表单提交,含多种数据上传
---恢复内容开始--- Ajax提交表单.使用FormData提交表单数据和上传的文件(这里的后台使用C#获取,你可以使用Java一样获取) 有时候前台的数据提交到后台,不想使用form表单上传,希 ...
- C#模拟POST表单提交 --- WebClient
string postString = "arg1=a&arg2=b";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进 ...
- 模拟post表单提交参数
Content-Type: application/x-www-form-urlencoded;charset=utf-8
随机推荐
- [转]PostgreSQL 中文资料汇总
原文链接:http://francs3.blog.163.com/blog/static/405767272014017341219/ --1 中文社区网站 PostgreSQL 中文社区官网: h ...
- HDOJ 2680 Dijkstra
题目大意: 给你一个有向图,一个起点集合,一个终点,求最短路.... 解题思路: 1.自己多加一个超级源点,把起点集合连接到超级源点上,然后将起点与超级源点的集合的路径长度设为0,这样就称为一个n+1 ...
- FireMonkey下的异形窗体拖动(句柄转换)
DelphiXE2 Firemoney FMX 的窗体不只是为windows的, 所以很多功能都没有了. 最常见的就是拖拽了 先看 VCL时代 一个经典拖动代码 ReleaseCapture(); S ...
- boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- java list三种遍历方法性能比较
从c/c++语言转向java开发,学习java语言list遍历的三种方法,顺便测试各种遍历方法的性能,测试方法为在ArrayList中插入1千万条记录,然后遍历ArrayList,发现了一个奇怪的现象 ...
- Jquery中的$().each,$.each的区别
在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...
- springMVC 使用jstl
jsp页面获取数据,感觉最方便的就是使用jstl+EL了,各种封装好的函数非常简单易用,接下来写如何使用jstl: 1.下载jstl-1.2_1.jar 2.由于项目是: xmlns="ht ...
- jsp 分页(数据库读取数据)
<%@ page contentType="text/html; charset=gb2312"%> <%@ page language="java&q ...
- Swift - 访问通讯录联系人(使用纯代码实现)
1,通讯录访问介绍 通讯录(或叫地址簿,电话簿)是一个数据库,里面储存了联系人的相关信息.要实现访问通讯录有如下两种方式: (1)AddressBook.framework框架 : 没有界面,通过代码 ...
- 实现文件下载的java代码
实现文件下载的java代码 //这是实现下载类(servlet),详细思路代码例如以下://也可连接数据库package com.message; import javax.servlet.*;imp ...