您的位置:首页 > 理论基础 > 计算机网络

使用socket发送HTTP请求的例子,

2012-03-02 11:53 295 查看
package com.jd.cis;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

/**
* 一个简单的HTTP客户端,发送HTTP请求,模拟浏览器
* 可打印服务器发送过来的HTTP消息
*/
public class SimpleHttpClient {
private static String encoding = "UTF-8";

public static void main(String[] args) {
try {
Socket s = new Socket("www.yuncheng.com", 80);
OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append("GET /searchcate.aspx?page=4&cate=%E5%9B%BE%E4%B9%A6&subcate=%E5%8E%9F%E5%88%9B%E6%96%87%E5%AD%A6&level3=%E7%8E%84%E5%B9%BB&level4=%E4%B8%9C%E6%96%B9%E7%8E%84%E5%B9%BB HTTP/1.1\r\n");
sb.append("Host: www.yuncheng.com\r\n");
// sb.append("GET /searchcate.aspx?page=4&cate=%E5%9B%BE%E4%B9%A6&subcate=%E5%8E%9F%E5%88%9B%E6%96%87%E5%AD%A6&level3=%E7%8E%84%E5%B9%BB&level4=%E4%B8%9C%E6%96%B9%E7%8E%84%E5%B9%BB#hd HTTP/1.1\r\n");
sb.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2\r\n");
sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
// sb.append("Accept-Encoding: gzip, deflate");
sb.append("Cookie: b_t_s=t130581851564xs; __utma=117338766.327361156.1330581863.1330589503.1330652535.4; __utmz=117338766.1330581863.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _jzqa=1.1052102171955060100.1330581863.1330589503.1330652535.4; SearchResult=32fc7b0d2d9b2f30ae5dd0e15cb22e62; beacon_visit_count=1; __utmb=117338766.1.10.1330652535; __utmc=117338766; _jzqb=1.1.10.1330652535.1; _jzqc=1\r\n");
sb.append("Cache-Control: max-age=0\r\n");
sb.append("Connection: Keep-Alive\r\n");

//注,这是关键的关键,忘了这里让我搞了半个小时。这里一定要一个回车换行,表示消息头完,不然服务器会等待
sb.append("\r\n");
osw.write(sb.toString());
osw.flush();

//--输出服务器传回的消息的头信息
InputStream is = s.getInputStream();

// byte[] aa=unzip(is);
// String html=new String(aa, encoding);
// System.out.println(html);

String line = null;
int contentLength = 0;//服务器发送回来的消息长度
// 读取所有服务器发送过来的请求参数头部信息
do {
line = readLine(is, 0);
//如果有Content-Length消息头时取出
if (line.startsWith("Content-Length")) {
contentLength = Integer.parseInt(line.split(":")[1].trim());
}
//打印请求部信息
System.out.print(line);
//如果遇到了一个单独的回车换行,则表示请求头结束
} while (!line.equals("\r\n"));

//--输消息的体
System.out.print(readLine(is, contentLength));

//关闭流
is.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/*
* 这里我们自己模拟读取一行,因为如果使用API中的BufferedReader时,它是读取到一个回车换行后
* 才返回,否则如果没有读取,则一直阻塞,直接服务器超时自动关闭为止,如果此时还使用BufferedReader
* 来读时,因为读到最后一行时,最后一行后不会有回车换行符,所以就会等待。如果使用服务器发送回来的
* 消息头里的Content-Length来截取消息体,这样就不会阻塞
*
* contentLe 参数 如果为0时,表示读头,读时我们还是一行一行的返回;如果不为0,表示读消息体,
* 时我们根据消息体的长度来读完消息体后,客户端自动关闭流,这样不用先到服务器超时来关闭。
*/
private static String readLine(InputStream is, int contentLe) throws IOException {
ArrayList lineByteList = new ArrayList();
byte readByte;
int total = 0;
if (contentLe != 0) {
do {
readByte = (byte) is.read();
lineByteList.add(Byte.valueOf(readByte));
total++;
} while (total < contentLe);//消息体读还未读完
} else {
do {
readByte = (byte) is.read();
lineByteList.add(Byte.valueOf(readByte));
} while (readByte != 10);
}

byte[] tmpByteArr = new byte[lineByteList.size()];
for (int i = 0; i < lineByteList.size(); i++) {
tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue();
}
lineByteList.clear();

return new String(tmpByteArr, encoding);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: