📄 httpclientconnection.java
字号:
/**
*
*/
package edu.sysu.http.impl;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import edu.sysu.http.intf.HttpConnection;
import edu.sysu.http.util.HttpGrammarException;
/**
* @author Administrator
*
*/
public class HttpClientConnection implements HttpConnection {
private BufferedReader input;
private BufferedWriter output;
private Socket socket = new Socket();
/**
*
*/
public HttpClientConnection() {
// TODO Auto-generated constructor stub
}
/**
*
*/
public HttpClientConnection(String host, int port, int timeout)
throws IOException {
// TODO Auto-generated constructor stub
if (host == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (port == 0) {
throw new IllegalArgumentException("Port may not be zero");
}
this.connect(host, port, timeout);
this.setInput(new BufferedReader(new InputStreamReader(this.socket
.getInputStream())));
this.setOutput(new BufferedWriter(new OutputStreamWriter(this.socket
.getOutputStream())));
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
if (!this.isClosed())
this.socket.close();
}
@Override
public void bind(String host, int port) throws IOException {
// TODO Auto-generated method stub
if (host == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (port == 0) {
throw new IllegalArgumentException("Port may not be zero");
}
this.socket.bind(new InetSocketAddress(host, port));
}
public void connect(String host, int port, int timeout) throws IOException {
// TODO Auto-generated method stub
if (host == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (port == 0) {
throw new IllegalArgumentException("Port may not be zero");
}
this.socket.connect(new InetSocketAddress(host, port), timeout);
}
@Override
public String getHost() {
// TODO Auto-generated method stub
return this.socket.getLocalAddress().getHostAddress();
}
@Override
public int getPort() {
// TODO Auto-generated method stub
return this.socket.getPort();
}
@Override
public boolean isClosed() {
// TODO Auto-generated method stub
return this.socket.isClosed();
}
public boolean isConnected(){
return this.socket.isConnected();
}
public int getSoTimeout() throws SocketException {
// TODO Auto-generated method stub
return this.socket.getSoTimeout();
}
public void setSoTimeout(int timeout) throws SocketException {
// TODO Auto-generated method stub
this.socket.setSoTimeout(timeout);
}
public void send(HttpRequest request) throws IOException {
this.output.write(request.toString(), 0, request.toString().length());
this.output.flush();
}
public String receive() throws IOException {
return this.input.readLine();
}
public BufferedReader getInput() {
return input;
}
public void setInput(BufferedReader input) {
this.input = input;
}
public BufferedWriter getOutput() {
return output;
}
public void setOutput(BufferedWriter output) {
this.output = output;
}
/*
* 解析服务器的响应
*/
public HttpResponse parseResponse() throws IOException,
HttpGrammarException {
HttpResponse response = null;
String tmp = this.receive();
if (tmp != null) {
response = new HttpResponse();
String[] res = tmp.split("\\" + HttpRules.SP);
HttpStatusLine statusLine = new HttpStatusLine(res[0]
.substring(res[0].lastIndexOf("/") + 1), res[1], res[2]);
response.setStatusLine(statusLine);
tmp = this.receive();
while (tmp != null && tmp.contains(":")) {
res = tmp.split(":");
response.getHeaderItems().add(new HttpHeader(res[0], res[1]));
tmp = this.receive();
}
String entity = new String();
while (tmp != null) {
entity += tmp + HttpRules.CR + HttpRules.LF;
try{
tmp = this.receive();
}catch(IOException e){
tmp = null;
continue;
}
}
if (entity != null) {
response.setEntity(new HttpEntity(new ByteArrayInputStream(
entity.toString().getBytes())));
}
} else
throw new IOException("Response message invalid.");
return response;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = System.getProperty("user.dir") + "\\WebRoot\\index.html";
System.out.println(path);
HttpResponse msg = new HttpResponse();
try {
msg.setStatusLine(new HttpStatusLine("1.1", HttpStatus.SC_OK
.getStatusCode(), HttpStatus.SC_OK.getReasonPhrase()));
msg.getEntity().readFrom(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpGrammarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpEntityHeader header1 = new HttpEntityHeader("Content-Length",
msg.getEntity().getLength().toString());
HttpEntityHeader header2 = new HttpEntityHeader("Content-Type", msg
.getEntity().getContentType().toString());
msg.getHeaderItems().add(header1);
msg.getHeaderItems().add(header2);
// System.out.println(msg.toString());
HttpClientConnection conn = new HttpClientConnection();
conn.setInput(new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(msg.toString().getBytes()))));
try {
HttpResponse response = conn.parseResponse();
System.out.println(response.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (HttpGrammarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -