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
随机推荐
- html+css实现图片的层布局
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Agg vs. Cairo 二维绘图引擎之比较和选择 .
Agg vs. Cairo 二维绘图引擎之比较和选择 cheungmine 当今时代对于作为二维图形软件开发者, 是幸运的.因为除了Windows GDI/GDI+之外,我们还有很多其他的选择.而且这 ...
- android代码控制seekbar的样式
package com.zte; import android.app.Activity; import android.graphics.Color; import android.graphics ...
- ssh登录的时候,根本不给输入密码的机会,直接拒绝,是因为BatchMode的设置
BatchMode no“BatchMode”如果设为“yes”,passphrase/password(交互式输入口令)的提示将被禁止.当不能交互式输入口令的时候,这个选项对脚本文件和批处理任务十分 ...
- Pro Android 4 第五章 理解Intent
Android引入了一个名为Intent的概念用来唤醒各种组件.Android中的组件包括:activities(UI 组件),services(后台代码),broadcast receiv ...
- android HorizontalListView
最近搞android 用到一个HorizontalListView 网上搜了一把.有一些国人实现的.但也有一些基本上是cp人家的开源项目. 本人找了两个.记录一下. 其一:https://github ...
- ShareSDK第三方登陆 (IOS)
1.http://www.mob.com/ 注册申请 2.http://www.mob.com/#/download SDK下载 (简洁版:http://www.mob.com/#/download ...
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
转载请注明出处http://blog.csdn.net/xiaanming/article/details/11380619 这一篇文章主要讲解的是百度地图的定位功能,然后还有MyLocationOv ...
- C语言总结之---关键字
我记得我开始学习C语言的时候,那时候还在读高中,我们老师就把C语言的关键字,全部写在黑板上,老师说我们下面的两节课的内容就是(把它给记下来) 你还记得标准C有多少个关键字吗? 第一:关键字描述 C99 ...
- hdu4707 Pet
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...