package ssd8.socket.http;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;
/**
* Class <em>HttpClient</em> is a class representing a simple HTTP client.
*
* @author wben
*/
public class HttpClient {
/**
* default HTTP port is port 80
*/
private static int port = 80;
/**
* Allow a maximum buffer size of 8192 bytes
*/
private static int buffer_size = 8192;
/**
* Response is stored in a byte array.
*/
private byte[] buffer;
/**
* My socket to the world.
*/
Socket socket = null;
/**
* Default port is 80.
*/
private static final int PORT = 80;
/**
* Output stream to the socket.
*/
BufferedOutputStream ostream = null;
/**
* Input stream from the socket.
*/
BufferedInputStream istream = null;
/**
* StringBuffer storing the header
*/
private StringBuffer header = null;
/**
* StringBuffer storing the response.
*/
private StringBuffer response = null;
/**
* String to represent the Carriage Return and Line Feed character sequence.
*/
static private String CRLF = "\r\n";
static private String Root="E:/JavaWorkplace/Exercise2/";
/**
* HttpClient constructor;
*/
public HttpClient() {
buffer = new byte[buffer_size];
header = new StringBuffer();
response = new StringBuffer();
}
/**
* <em>connect</em> connects to the input host on the default http port --
* port 80. This function opens the socket and creates the input and output
* streams used for communication.
*/
public void connect(String host) throws Exception {
/**
* Open my socket to the specified host at the default port.
*/
socket = new Socket(host, PORT);
/**
* Create the output stream.
*/
ostream = new BufferedOutputStream(socket.getOutputStream());
/**
* Create the input stream.
*/
istream = new BufferedInputStream(socket.getInputStream());
}
/**
* <em>processGetRequest</em> process the input GET request.
*/
public void processGetRequest(String request) throws Exception {
/**
* Send the request to the server.
*/
request += CRLF + CRLF;
buffer = request.getBytes();
ostream.write(buffer, 0, request.length());
ostream.flush();
/**
* waiting for the response.
*/
processResponse();
}
/**
* <em>processPutRequest</em> process the input PUT request.
*/
public void processPutRequest(String request) throws Exception {
//=======start your job here============//
if(request!=null&&request.startsWith("PUT"))
{
String fName;
if(request.startsWith("/"))
fName=request.substring(5, request.length()-9);
else {
fName=request.substring(4,request.length()-9);
}
System.out.println(fName);
File file=new File(Root+fName);
if(file.exists()&&!file.isDirectory())
{
String reqMessage=request+CRLF;
reqMessage+="Accept: */*"+CRLF;
reqMessage+="Accept-Language: zh-cn"+CRLF;
reqMessage+="Content-Length: "+file.length()+CRLF;
if(fName.endsWith("txt")||fName.endsWith("htm")||fName.endsWith("html"))
reqMessage+="Content-Type: text/html"+CRLF;
else if(fName.endsWith("jpg")||fName.endsWith("jpeg")) {
reqMessage+="Content-Type: image/jpeg"+CRLF;
}
else {
reqMessage+="Content-Type: application/octet-stream"+CRLF;
}
reqMessage+=CRLF;
System.out.println(reqMessage);
buffer=reqMessage.getBytes();
ostream.write(buffer);
FileInputStream in = new FileInputStream(file);
System.out.println(in.available());
/*StringBuffer stringBuffer=new StringBuffer();
*/while(in.read(buffer) != -1)
{
ostream.write(buffer);
/*stringBuffer.append(new String(buffer));*/
}
/*ostream.write(stringBuffer.toString().getBytes("iso-8859-1"));
*//*System.out.println(stringBuffer);
*/ostream.flush();
in.close();
}
else {
System.err.println("文件不存在");
}
}
System.out.println("llllll");
processResponse();
//=======end of your job============//
}
/**
* <em>processResponse</em> process the server response.
*
*/
public void processResponse() throws Exception {
int last = 0, c = 0;
/**
* Process the header and add it to the header StringBuffer.
*/
boolean inHeader = true; // loop control
while (inHeader && ((c = istream.read()) != -1)) {
switch (c) {
case '\r':
break;
case '\n':
if (c == last) {
inHeader = false;
break;
}
last = c;
header.append("\n");
break;
default:
last = c;
header.append((char) c);
}
}
System.out.println("Header:"+header.toString());
/**
* Read the contents and add it to the response StringBuffer.
*/
while (istream.available()>0) {
istream.read(buffer);
response.append(new String(buffer,"iso-8859-1"));
}
System.out.println("body:"+response);
}
/**
* Get the response header.
*/
public String getHeader() {
return header.toString();
}
/**
* Get the server's response.
*/
public String getResponse() {
return response.toString();
}
/**
* Close all open connections -- sockets and streams.
*/
public void close() throws Exception {
socket.close();
istream.close();
ostream.close();
}
}
西工大网络分布式实验二
需积分: 10 75 浏览量
2018-12-10
23:19:12
上传
评论
收藏 7KB RAR 举报
WuHoby
- 粉丝: 0
- 资源: 1
最新资源
- sony 索尼IMX334摄像头模组电路板AD版硬件PCB图(6层板).zip
- 基于flask和echarts融合交易策略的bitfinex可视化微服务.zip
- 包含了wvp-assist.tar wvp-talk.tar zlmediakit.tar .
- 3r4efgh53wgrf43tw
- 2024新版Java基础从入门到精通全套视频+资料下载
- Spring AI大模型视频教程+ChatGPT视频教程+OpenAI大模型视频教程(资料+视频教程)
- ABB工业机器人教程PDF版本
- 123321123323211
- yolov8实战第八天-pyqt5-yolov8实现车牌识别系统(论文(8700+字+数据集+完整部署代码+代码使用说明)
- 三相桥式全桥整流电路MATALB Simulink仿真文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈