import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class Client
{
public static void test(String url,String encode,String path)
{
HttpClient httpClient = new HttpClient();
// httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort) 可设置代理
// post方法
PostMethod postMethod = new PostMethod(url);
String from = "广州";
String to = "成都";
//需要的参数名称和值
NameValuePair[] paras = new NameValuePair[3];
paras[0] = new NameValuePair("from", from);
paras[1] = new NameValuePair("to",to);
paras[2] = new NameValuePair("act", "3");
postMethod.addParameters(paras);
postMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset="+encode);
postMethod.setRequestHeader("Referer", "http://www.ttkdex.com/");
BufferedReader reader = null;
BufferedWriter writer = null;
try
{
//响应状态码 成功为200
int code = httpClient.executeMethod(postMethod);
if(HttpStatus.SC_OK != code)
{
System.out.println("响应不成功");
return;
}
System.out.println("响应成功:code="+code);
reader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), encode));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encode));
String line = null;
while ((line = reader.readLine()) != null)
{
writer.write(line+"\r\n");
}
reader.close();
writer.close();
System.out.println("success ! result in file : "+path);
}
catch (HttpException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
//完成,释放连接,释放资源
postMethod.releaseConnection();
try
{
if(reader != null)
{
reader.close();
}
if(writer != null)
{
reader.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
System.out.println("run over ...");
}
public static void main(String[] args)
{
String url = "http://qq.ip138.com/train/train_search.asp";
String encode = "gb2312";
String savePath = "E:/test.html";
test(url,encode,savePath);
}
}
分享到:
相关推荐
HttpClient抓取网页Demo,HttpClient 的入门示例,配合blog文章的附件
httpClient+jsoup抓取网页数据实例和jar包
java httpclient 抓取 数据 和jar 包
HttpClient网页抓取工具包整合,
HttpClient用法,实例 HttpClient用法,实例 HttpClient用法,实例 HttpClient用法,实例
NULL 博文链接:https://houyajie.iteye.com/blog/2433840
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
httpclient远程网页抓取工具,可以继承到web项目工程中,很好哟,还等什么?
Java抓取https网页数据,解决peer not authenticated异常。导入eclipse就能运行,带有所用的jar包(commons-httpclient-3.1.jar,commons-logging.jar,httpclient-4.2.5.jar,httpcore-4.2.4.jar)
NULL 博文链接:https://jilongliang.iteye.com/blog/1275107
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
利用HttpClient抓取网页中简单的数据,数据可存入数据库进行分析
利用httpClient+jsoup技术进行网页数据的获取,以网易贵金属为例~
Android的HttpClient开发实例。欢迎下载学习
自己做的httpClient实例,写的比较多刚接触的可以看看
本人做得一个httpClient的 网络传输的协议,由客户端和服务端,直接可以用
NULL 博文链接:https://shaozhen.iteye.com/blog/950472
JavaHttpClient实例
一个java web project的压缩包。 里面是一个标准的httpclient测试类。 包含3个必备的jar包。 导入myeclipse即可使用。