java使用socket实现http简单get请求
2013-02-26 20:14
633 查看
Java代码
package com.hs;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SimpleHttpGet {
public static
final String SEQUENCE = "\r\n";
public static
void main(String[] args) throws UnknownHostException,
IOException {
String host = "www.baidu.com";
Socket socket = new Socket(host,
80);
OutputStream os = socket.getOutputStream();
StringBuffer head = new StringBuffer();
// 这些是必须的
head.append("GET / HTTP/1.1" + SEQUENCE);
head.append("Host:" + host + SEQUENCE + SEQUENCE);
// 这些是可选的
head.append("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
head.append("Accept-Language:zh-CN,zh;q=0.8");
head.append("User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
os.write(head.toString().getBytes());
os.flush();
InputStream is = socket.getInputStream();
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
StringBuffer headRes = new StringBuffer();
String line = null;
int contentLength = 0;
do {
line = lnr.readLine();
headRes.append(line + SEQUENCE);
if (line.startsWith("Content-Length")) {
contentLength = Integer.parseInt(line.split(":")[1].trim());
}
// 由于LineNumberReader会把\r\n替换掉,所以如果读到一行为""证明http head结束
} while (!line.equals(""));
int totalCount = 0;
byte[] buff = new
byte[256];
StringBuffer contentRes = new StringBuffer();
while (totalCount < contentLength) {
int len = is.read(buff);
totalCount += len;
contentRes.append(new String(buff,
0, len, "gbk"));
}
System.out.println(headRes.toString());
System.out.println(contentRes.toString());
socket.close();
}
}
只实现了get请求,而且如果http head中没有 content-length也会有问题, 比如有的网站头中有
Transfer-Encoding:
chunked ,这样就会没有content-length信息, 这个我也没解决, 如果谁有兴趣可以以深入了解下http协议头中的
Transfer-Encoding:
chunked代表什么意思。
package com.hs;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SimpleHttpGet {
public static
final String SEQUENCE = "\r\n";
public static
void main(String[] args) throws UnknownHostException,
IOException {
String host = "www.baidu.com";
Socket socket = new Socket(host,
80);
OutputStream os = socket.getOutputStream();
StringBuffer head = new StringBuffer();
// 这些是必须的
head.append("GET / HTTP/1.1" + SEQUENCE);
head.append("Host:" + host + SEQUENCE + SEQUENCE);
// 这些是可选的
head.append("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
head.append("Accept-Language:zh-CN,zh;q=0.8");
head.append("User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
os.write(head.toString().getBytes());
os.flush();
InputStream is = socket.getInputStream();
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
StringBuffer headRes = new StringBuffer();
String line = null;
int contentLength = 0;
do {
line = lnr.readLine();
headRes.append(line + SEQUENCE);
if (line.startsWith("Content-Length")) {
contentLength = Integer.parseInt(line.split(":")[1].trim());
}
// 由于LineNumberReader会把\r\n替换掉,所以如果读到一行为""证明http head结束
} while (!line.equals(""));
int totalCount = 0;
byte[] buff = new
byte[256];
StringBuffer contentRes = new StringBuffer();
while (totalCount < contentLength) {
int len = is.read(buff);
totalCount += len;
contentRes.append(new String(buff,
0, len, "gbk"));
}
System.out.println(headRes.toString());
System.out.println(contentRes.toString());
socket.close();
}
}
package com.hs; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class SimpleHttpGet { public static final String SEQUENCE = "\r\n"; public static void main(String[] args) throws UnknownHostException, IOException { String host = "www.baidu.com"; Socket socket = new Socket(host, 80); OutputStream os = socket.getOutputStream(); StringBuffer head = new StringBuffer(); // 这些是必须的 head.append("GET / HTTP/1.1" + SEQUENCE); head.append("Host:" + host + SEQUENCE + SEQUENCE); // 这些是可选的 head.append("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); head.append("Accept-Language:zh-CN,zh;q=0.8"); head.append("User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); os.write(head.toString().getBytes()); os.flush(); InputStream is = socket.getInputStream(); LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is)); StringBuffer headRes = new StringBuffer(); String line = null; int contentLength = 0; do { line = lnr.readLine(); headRes.append(line + SEQUENCE); if (line.startsWith("Content-Length")) { contentLength = Integer.parseInt(line.split(":")[1].trim()); } // 由于LineNumberReader会把\r\n替换掉,所以如果读到一行为""证明http head结束 } while (!line.equals("")); int totalCount = 0; byte[] buff = new byte[256]; StringBuffer contentRes = new StringBuffer(); while (totalCount < contentLength) { int len = is.read(buff); totalCount += len; contentRes.append(new String(buff, 0, len, "gbk")); } System.out.println(headRes.toString()); System.out.println(contentRes.toString()); socket.close(); } }
只实现了get请求,而且如果http head中没有 content-length也会有问题, 比如有的网站头中有
Transfer-Encoding:
chunked ,这样就会没有content-length信息, 这个我也没解决, 如果谁有兴趣可以以深入了解下http协议头中的
Transfer-Encoding:
chunked代表什么意思。
相关文章推荐
- java最简单的方式实现http get请求 gbk转utf8 javaweb发布到服务器
- Java使用HttpURLConnection发送Get及Post请求简单过程
- 练习使用 socket 发送简单的 HTTP GET 请求
- 简单记下httpclientjava实现get,post请求
- java实现http的get和post请求
- JAVA编写的使用Socket模拟Http的GET操作
- java实现http的Post、Get、代理访问请求
- Java实现http get post请求
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- 用Java实现发送http的get和post请求
- Java使用HttpURLConnection请求异常:java.net.SocketException: Software caused connection abort: recv failed
- 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- JAVA使用http发送post或get请求实例
- 使用SOCKET 发送HTTP1.1 GET POST请求包
- [Java学习] java实现http的Post、Get、代理访问请求
- java HTTP get post请求,获取二进制文件实现
- 使用Socket发HTTP GET请求
- c#使用Socket发送HTTP/HTTPS请求的实现代码
- Java 模拟HTTP Get Post请求 校园BBS自动回帖实现
- socket编程---实现get post 向http发送请求