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 ...
随机推荐
- IOS 判断设备类型
- (NSString*)deviceString { // 需要#import "sys/utsname.h" struct utsname systemInfo; uname( ...
- keil将程序装入外部FLASH具体解释
在实际项目中,常常出现芯片的内部FLASH空间不够的情况,这就须要将程序分一部分装到外部FLASH中. 为了让大家能少走些弯路,在这里把我在这当中遇到的一些问题和经验教训给大家分享一下. 仅供參考,假 ...
- 使用maven编译的时候提示 maven-source 1.3 中不支持注释请使用 -source 5 或更高版本以启用注释的错误。
在编译的模块的pom文件中加上 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins ...
- Android编译过程详解(三)
前面两节讲解了自定义Android编译项和创建Product产品配置文件,除了编译和定义产品相关环境变量外,还需要定义Board相关环境变量. 1. build/core/config.mk 109 ...
- python os模块文件相关
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- oracle行号排序问题
1.创建一个student,并且插入数据 ),age int) '); '); '); '); '); commit; 2.直接按照age进行排序显示行号: select * from(select ...
- build opencv with python support
cmake -DPYTHON_LIBRARY=/opt/anaconda/lib/libpython2.7.so -DPYTHON_INCLUDE_DIR=/opt/anaconda/include/ ...
- (原)编译caffe时提示未定义的引用(undefined reference to)
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5864715.html 参考网址: https://github.com/BVLC/caffe/issu ...
- (原)ubuntu16中安装moses
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5653186.html 在ubuntu14中,可以使用下面的语句安装moses: luarocks in ...
- Java的Date类与Calendar类
一:Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Da ...