java模拟get/post提交
1:用jdk连接
String action = "xxxxxxxxxxx";
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setConnectTimeout(0);
http.setInstanceFollowRedirects(true);
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDefaultUseCaches(false);
http.setDoOutput(true); String queryString = "";
PrintWriter out = new PrintWriter(http.getOutputStream());
out.print(queryString);//传入参数
out.close();
http.connect();//连接
InputStream in = httpURLConnection.getInputStream();
2:apache组件
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Map;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpMethod;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.URIException;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.params.HttpMethodParams;
- import org.apache.commons.httpclient.util.URIUtil;
- /**
- *
- *
- * <p>Title:HttpTookitEnhance</p>
- * <p>Description: httpclient模拟http请求,解决返回内容乱码问题</p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author libin
- * @version 1.0.0
- */
- public class HttpTookitEnhance
- {
- /**
- * 执行一个HTTP GET请求,返回请求响应的HTML
- *
- * @param url 请求的URL地址
- * @param queryString 请求的查询参数,可以为null
- * @param charset 字符集
- * @param pretty 是否美化
- * @return 返回请求响应的HTML
- */
- public static String doGet ( String url, String queryString, String charset, boolean pretty )
- {
- StringBuffer response = new StringBuffer();
- HttpClient client = new HttpClient();
- GetMethodmethod = new GetMethod(url);
- try
- {
- if ( queryString != null && !queryString.equals("") )
- //对get请求参数做了http请求默认编码,好像没有任何问题,汉字编码后,就成为%式样的字符串
- method.setQueryString(URIUtil.encodeQuery(queryString));
- client.executeMethod(method);
- if ( method.getStatusCode() == HttpStatus.SC_OK )
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
- String line;
- while ( ( line = reader.readLine() ) != null )
- {
- if ( pretty )
- response.append(line).append(System.getProperty("line.separator"));
- else
- response.append(line);
- }
- reader.close();
- }
- }
- catch ( URIException e )
- {
- }
- catch ( IOException e )
- {
- }
- finally
- {
- method.releaseConnection();
- }
- return response.toString();
- }
- /**
- * 执行一个HTTP POST请求,返回请求响应的HTML
- *
- * @param url 请求的URL地址
- * @param params 请求的查询参数,可以为null
- * @param charset 字符集
- * @param pretty 是否美化
- * @return 返回请求响应的HTML
- */
- public static String doPost ( String url, Map<String, String> params, String charset, boolean pretty )
- {
- StringBuffer response = new StringBuffer();
- HttpClient client = new HttpClient();
- PostMethodmethod = new PostMethod(url);
- //设置Http Post数据
- if ( params != null )
- {
- HttpMethodParams p = new HttpMethodParams();
- for ( Map.Entry<String, String> entry : params.entrySet() )
- {
- p.setParameter(entry.getKey(), entry.getValue());
- }
- method.setParams(p);
- }
- try
- {
- client.executeMethod(method);
- if ( method.getStatusCode() == HttpStatus.SC_OK )
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
- String line;
- while ( ( line = reader.readLine() ) != null )
- {
- if ( pretty )
- response.append(line).append(System.getProperty("line.separator"));
- else
- response.append(line);
- }
- reader.close();
- }
- }
- catch ( IOException e )
- {
- }
- finally
- {
- method.releaseConnection();
- }
- return response.toString();
- }
- public static void main ( String [] args )
- {
- String y = doGet("http://video.sina.com.cn/life/tips.html", null, "GBK", true);
- System.out.println(y);
- }
- }
java模拟get/post提交的更多相关文章
- java模拟post方式提交表单实现图片上传【转】
转自:http://blog.csdn.net/5iasp/article/details/8669644 模拟表单html如下: <form action="up_result ...
- JAVA模拟表单提交
这是我网上搜的,自己使用也蛮方便,所以上传供大家分享. package wzh.Http; import java.io.BufferedReader; import java.io.IOExce ...
- 真理胜于一切 JAVA模拟表单提交
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- Java模拟HttpClient进行Get和Post提交
使用Java模拟客户端进行提交,需要用到apache http client jar,这里用的是4.4版本 GET: public void GetURL(){ String strResp=&qu ...
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...
- Java模拟登陆02【转载】
在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...
- java模拟form上传数据
Java模拟form表单上传 查看form表单提交的http请求为 import java.io.*; import java.net.*; public class FileUpload { /** ...
- HTTP通信模拟表单提交数据
前面记录过一篇关于http通信,发送数据的文章:http://www.cnblogs.com/hyyq/p/7089040.html,今天要记录的是如何通过http模拟表单提交数据. 一.通过GET请 ...
- java模拟Cookies登陆
在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时时将co ...
随机推荐
- Python 异步IO、IO多路复用
事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 国内ip信息库的组建
1.从 APNIC 分析得到国内的段 数据源位置:http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest 2.从QQ纯真库分析得到国 ...
- 《JavaScript 闯关记》之原型及原型链
原型链是一种机制,指的是 JavaScript 每个对象都有一个内置的 __proto__ 属性指向创建它的构造函数的 prototype(原型)属性.原型链的作用是为了实现对象的继承,要理解原型链, ...
- HDU 5618 Jam's problem again
题意: 三维坐标,对于1个点,找出有多少个点,3个坐标都比该点小! Sample Input 1 4 10 4 7 10 6 6 8 2 5 7 3 10 Sample Output 1 1 0 ...
- setNeedsDisplay、layoutSubViews
UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.而setNeedsDisplay会调 用自动调用drawRect方法,这样可以拿到UIGra ...
- SQL重复记录查询的几种方法(转)
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people ) 2.删除表中多余的重复记录,重复记录是根据单个字段(people ...
- uva 1595 Symmetry“结构体”
给出平面上N(N<=1000)个点.问是否可以找到一条竖线,使得所有点左右对称,如图所示: 则左边的图形有对称轴,右边没有. Sample Input 3 5 -2 5 0 0 6 5 4 ...
- git add和被ignore的文件
如果有如下的目录结构: workspace tree | --------------------- | | hello.c ...
- Image的Stride
参看下面链接:http://msdn.microsoft.com/en-us/library/aa473780
- JQuery easyui (2)Droppable(放置)组件
所谓放置,就是将一个物体放入一个物体内,当然对于easyui来说触发各种效果是必不可少的,同时这个组件也不会依赖于其他组件. Droppable的加载方式 1,class 加载 一直不太喜欢cl ...