HttpConnection模拟http请求获取网页内容
来源:互联网 发布:java web应用程序开发 编辑:程序博客网 时间:2024/05/13 10:53
package cn.org.nterc.gtzdms.utils;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
/**
* Created by IntelliJ IDEA.
* User: user
* Date: 2009-5-11
* Time: 13:34:10
* 模拟Http访问的工具类
*/
public class HttpConnectionUtil {
public static String getHttpContent(String url) {
return getHttpContent(url, "GB2312");
}
public static String getHttpContent(String url, String charSet) {
HttpURLConnection connection = null;
String content = "";
try {
URL address_url = new URL(url);
connection = (HttpURLConnection) address_url.openConnection();
// connection.setRequestMethod("GET");
//设置访问超时时间及读取网页流的超市时间,毫秒值
System.setProperty("sun.net.client.defaultConnectTimeout","30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
//after JDK 1.5
// connection.setConnectTimeout(10000);
// connection.setReadTimeout(10000);
//得到访问页面的返回值
int response_code = connection.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
InputStream in = connection.getInputStream();
// InputStreamReader reader = new InputStreamReader(in,charSet);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charSet));
String line = null;
while ((line = reader.readLine()) != null) {
content+=line;
}
return content;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(connection !=null){
connection.disconnect();
}
}
return "";
}
public static void main(String[] args) {
// String content = HttpConnectionUtil.getHttpContent("http://192.168.1.61:8001/");
String content = HttpConnectionUtil.getHttpContent("http://www.baidu.com");
System.out.println("content = " + content);
}
}
没啥说的
但是个人认为HttpClient是最佳选择,从功能和效率上都很强大
- HttpConnection模拟http请求获取网页内容
- 用curl模拟http请求获取网页
- 对http提出Get请求,获取网页内容
- http 请求获取页面内容
- vc 获取网页内容(HTTP)
- http获取网页内容类
- vc 获取网页内容(HTTP)
- 14 使用HttpConnection获取网络网页源代码
- 模拟http请求的网页调试工具
- mule logger显示http请求网页内容
- HttpConnection请求
- java 模拟http请求获取数据
- beego框架获取http请求内容
- Java 获取http请求头内容
- servlet获取http请求的内容
- 发送Http请求获取网页源码
- http--发送get请求获取网页
- HTTP 获取网页内容 HttpURLConnection与HttpClient
- linux shell
- SQL SERVER 触发器如何弹出对话框
- Java中的Big-Little问题的一种解决方法
- JDI thread
- USB总线协议概述
- HttpConnection模拟http请求获取网页内容
- glibc与MSVC CRT,crt编译错误及解决
- 我的输入法上哪去了?
- delphi 子窗体最大化
- prototype 属性使用说明
- Windows Mobile下WinInet的异步使用方法 (转)
- MSVC CRT的全局构造和析构(1)
- TDI code 1
- 工作