Java Http & Https请求客户端代码-直接基于Socket,不依赖其他第三方库

HttpInputStream

public class HttpInputStream extends InputStream {

private InputStream inputStream;

public HttpInputStream(InputStream inputStream) {

this.inputStream = inputStream;

}

@Override

public int read() throws IOException {

return inputStream.read();

}

public int read(byte b[]) throws IOException {

return inputStream.read(b);

}

public int read(byte b[], int off, int len) throws IOException {

return inputStream.read(b, off, len);

}

public long skip(long n) throws IOException {

return inputStream.skip(n);

}

public int available() throws IOException {

return inputStream.available();

}

public void close() throws IOException {

inputStream.close();

}

}

HttpOutputStream

public class HttpOutputStream extends OutputStream {

private OutputStream outputStream;

public HttpOutputStream(OutputStream outputStream) {

this.outputStream = outputStream;

}

@Override

public void write(int b) throws IOException {

outputStream.write(b);

}

public void write(byte b[]) throws IOException {

outputStream.write(b);

}

public void write(byte b[], int off, int len) throws IOException {

outputStream.write(b, off, len);

}

public void flush() throws IOException {

outputStream.flush();

}

public void close() throws IOException {

outputStream.close();

}

}

HttpConnection

public class HttpConnection {

private String host;

private int port;

private boolean secure;

private Socket socket;

private InputStream is;

private OutputStream os;

public HttpConnection(String host, int port) {

this(host, port, false);

}

public HttpConnection(String host, int port, boolean secure) {

this.host = host;

this.port = port;

this.secure = secure;

}

public void open() throws IOException {

if (secure) {

socket = SSLSocketFactory.getDefault().createSocket(host, port);

} else {

socket = new Socket(host, port);

}

is = new HttpInputStream(socket.getInputStream());

os = new HttpOutputStream(socket.getOutputStream());

}

public static HttpConnection getConnection(String host, int port) throws IOException {

HttpConnection connection = new HttpConnection(host, port);

connection.open();

return connection;

}

public static HttpConnection getConnection(String host, int port, boolean secure) throws IOException {

HttpConnection connection = new HttpConnection(host, port, secure);

connection.open();

return connection;

}

public InputStream getInputStream() {

return is;

}

public OutputStream getOutputStream() {

return os;

}

}

Http请求客户端代码:

@Test

public void http() throws IOException {

HttpConnection connection = HttpConnection.getConnection("www.baidu.com", 80);

try {

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

//writer.write("GET http://m.alaemall.com/default.shtml HTTP/1.0\r\n");

//writer.write("GET /default.shtml HTTP/1.0\r\n");

//writer.write("GET http://m.alaemall.com/default.shtml HTTP/1.1\r\n");

//writer.write("GET /default.shtml HTTP/1.1\r\n");

writer.write("GET / HTTP/1.1\r\n");

//writer.write("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");

//writer.write("Accept-Encoding:gzip,deflate,sdch\r\n");

//writer.write("Accept-Language:zh-CN,zh;q=0.8\r\n");

//writer.write("Cache-Control:max-age=0\r\n");

//writer.write("Connection:keep-alive\r\n");

//writer.write("Cookie:JSESSIONID=98F8CB4369EAD78CC321B2E5BD344191\r\n");

//writer.write("Host:m.alaemall.com\r\n");

//writer.write("Host:Host:m.alaemall.com\r\n");

//writer.write("Host:m.alaepay.com\r\n");

writer.write("Host:www.baidu.com\r\n");

//writer.write("Host: \r\n");

writer.write("\r\n");

writer.flush();

InputStream is = connection.getInputStream();

StringBuilder sb = new StringBuilder();

byte[] b = new byte[512];

int nb = -1;

while ((nb = is.read(b)) != -1) {

sb.append(new String(b, 0, nb));

}

System.out.println(sb.toString());

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

Https请求客户端代码:

@Test

public void https() throws IOException {

HttpConnection connection = HttpConnection.getConnection("www.baidu.com", 443, true);

try {

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

//writer.write("GET http://m.alaemall.com/default.shtml HTTP/1.0\r\n");

//writer.write("GET /default.shtml HTTP/1.0\r\n");

//writer.write("GET http://m.alaemall.com/default.shtml HTTP/1.1\r\n");

//writer.write("GET /default.shtml HTTP/1.1\r\n");

writer.write("GET / HTTP/1.1\r\n");

//writer.write("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");

//writer.write("Accept-Encoding:gzip,deflate,sdch\r\n");

//writer.write("Accept-Language:zh-CN,zh;q=0.8\r\n");

//writer.write("Cache-Control:max-age=0\r\n");

//writer.write("Connection:keep-alive\r\n");

//writer.write("Cookie:JSESSIONID=98F8CB4369EAD78CC321B2E5BD344191\r\n");

//writer.write("Host:m.alaemall.com\r\n");

//writer.write("Host:Host:m.alaemall.com\r\n");

//writer.write("Host:m.alaepay.com\r\n");

writer.write("Host:www.baidu.com\r\n");

//writer.write("Host: \r\n");

writer.write("\r\n");

writer.flush();

InputStream is = connection.getInputStream();

StringBuilder sb = new StringBuilder();

byte[] b = new byte[512];

int nb = -1;

while ((nb = is.read(b)) != -1) {

sb.append(new String(b, 0, nb));

}

System.out.println(sb.toString());

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

返回结果:

HTTP/1.1 200 OK

Server: bfe/1.0.8.18

Date: Thu, 29 Sep 2016 15:38:28 GMT

Content-Type: text/html

Content-Length: 14720

Connection: keep-alive

Last-Modified: Tue, 30 Aug 2016 09:16:00 GMT

Vary: Accept-Encoding

Set-Cookie: BAIDUID=FCC74F1440586BD1DE5DDDCCE6B4ABB5:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: BIDUPSID=FCC74F1440586BD1DE5DDDCCE6B4ABB5; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: PSTM=1475163508; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

P3P: CP=" OTI DSP COR IVA OUR IND COM "

X-UA-Compatible: IE=Edge,chrome=1

Pragma: no-cache

Cache-control: no-cache

Accept-Ranges: bytes

Set-Cookie: __bsi=14310701737733550345_00_263_N_N_1_0301_002F_N_N_N_0; expires=Thu, 29-Sep-16 15:38:33 GMT; domain=www.baidu.com; path=/

<!DOCTYPE html><!--STATUS OK-->

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

。。。。。。

猜你喜欢

转载自lobin.iteye.com/blog/2327949