用Socket类实现HTTP协议客户端应用
当前位置:以往代写 > JAVA 教程 >用Socket类实现HTTP协议客户端应用
2019-06-14

用Socket类实现HTTP协议客户端应用

用Socket类实现HTTP协议客户端应用

副标题#e#

Http客户端措施已集成在Java语言中,可以通过URLConnection类挪用。遗憾的是,由于SUN没有发布Http客户措施的源码,它实现的细节仍是一个谜。本文按照HTTP协议类型,用Java.net.Socket类实现一个HTTP协议客户端措施。

1.Socket类:

相识TCP/IP协议集通信的读者知道,协议间的通信是通过Socket完成的。在Java.net包中,Socket类就是对Socket的详细实现。它通过毗连到主机后,返回一个I/O流,实现协议间的信息互换。

2 . HTTP协议

HTTP协议同其它TCP/IP协议会合的协议一样,是遵循客户/处事器模子事情的。客户端发往处事端的信息名目如下:

请求要领 URL HTTP协议的版本号

提交的元信息

**空行**

实体

请求要领是对这次毗连事情的说明,今朝HTTP协议已经成长到1.1版,它包罗GET、HEAD、POST、DELETE、OPTIONS、TRACE、PUT七种。元信息是关于当前请求的信息。通过阐明元信息,可以查抄实体数据是否完整,吸收进程是否堕落,范例是否匹配等。元信息的引入使HTTP协议通信越发稳妥靠得住。实体是请求的详细内容。

将上述报文发往Web处事器,假如乐成,应答名目如下:

HTTP协议的版本号 应答状态码 应答状态码说明

吸收的元信息

**空行**

实体

以上报文发向客户端,而且吸收乐成,互相间封锁毗连,完成一次握手。下面用最常用的GET要领,来说明详细的报文应用:

GET http://www.youhost.com HTTP/1.0
accept: www/source; text/html; image/gif; image/jpeg; */*
User_Agent: myAgent
**空行**

这个报文是向www.youhost.com主机请求一个缺省HTML文档。客户端HTTP协议版本号是1.0版,元信息包罗可吸收的文件名目,用户署理,每一段之间用回车换行符脱离,最后以一个空行竣事。发向处事器后,假如执行进程正常,处事器返回以下代码:

HTTP/1.1 200 OK

Date: Tue, 14 Sep 1999 02:19:57 GMT

Server: Apache/1.2.6

Connection: close

Content-Type: text/html

**空行**

<html><head>…</head><body>…</body></html>

HTTP/1.1暗示这个HTTP处事器是1.1版,200是处事器对客户请求的应答状态码,OK是对应答状态码的表明,之后是这个文档的元信息和文档正文。(相关应答状态码和元信息的表明请参阅Inetrnet尺度草案:RFC2616)。


#p#副标题#e#

3. HTTP客户端措施:

import java.net.*;
import java.io.*;
import java.util.Properties;
import java.util.Enumeration;
public class Http {
protected Socket client;
protected BufferedOutputStream sender;
protected BufferedInputStream receiver;
protected ByteArrayInputStream byteStream;
protected URL target;
private int responseCode=-1;
private String responseMessage="";
private String serverVersion="";
private Properties header = new Properties();
public Http() { }
public Http(String url) {
GET(url) ;
}
/* GET要领按照URL,会请求文件、数据库查询功效、措施运行功效等多种内容 */
public void GET(String url) {
try {
checkHTTP(url);
openServer(target.getHost(),target.getPort() );
String cmd = "GET "+ getURLFormat(target) +" HTTP/1.0\r\n"
+ getBaseHeads()+"\r\n";
sendMessage(cmd);
receiveMessage();
}catch(ProtocolException p) {
p.printStackTrace();
return;
}catch(UnknownHostException e) {
e.printStackTrace();
return;
}catch(IOException i)
i.printStackTrace();
return;
}
}
/*
* HEAD要领只请求URL的元信息,不包罗URL自己。若猜疑本机和处事器上的
* 文件沟通,用这个要领查抄最快捷有效。
*/
public void HEAD(String url) {
try {
checkHTTP(url);
openServer(target.getHost(),target.getPort() );
String cmd = "HEAD "+getURLFormat(target)+" HTTP/1.0\r\n"
+getBaseHeads()+"\r\n";
sendMessage(cmd);
receiveMessage();
}catch(ProtocolException p) {
p.printStackTrace();
return;
}catch(UnknownHostException e) {
e.printStackTrace();
return;
}catch(IOException i)
i.printStackTrace();
return;
}
}
/*
* POST要领是向处事器传送数据,以便处事器做出相应的处理惩罚。譬喻网页上常用的
* 提交表格。
*/
public void POST(String url,String content) {
try {
checkHTTP(url);
openServer(target.getHost(),target.getPort() );
String cmd = "POST "+ getURLFormat(target) +"
HTTP/1.0\r\n"+getBaseHeads();
cmd += "Content-type: application/x-www-form-urlencoded\r\n";
cmd += "Content-length: " + content.length() + "\r\n\r\n";
cmd += content+"\r\n";
sendMessage(cmd);
receiveMessage();
}catch(ProtocolException p) {
p.printStackTrace();
return;
}catch(UnknownHostException e) {
e.printStackTrace();
return;
}catch(IOException i)
i.printStackTrace();
return;
}
}
protected void checkHTTP(String url) throws ProtocolException {
try {
URL target = new URL(url);
if(target==null || !target.getProtocol().toUpperCase().equals("HTTP") )
throw new ProtocolException("这不是HTTP协议");
this.target = target;
}catch(MalformedURLException m) {
throw new ProtocolException("协议名目错误");
}
}
/*
* 与Web处事器毗连。若找不到Web处事器,InetAddress会激发UnknownHostException
* 异常。若Socket毗连失败,会激发IOException异常。
*/
protected void openServer(String host,int port) throws
UnknownHostException,IOException {
header.clear();
responseMessage=""; responseCode=-1;
try {
if(client!=null) closeServer();
if(byteStream != null) {
byteStream.close(); byteStream=null;
}
InetAddress address = InetAddress.getByName(host);
client = new Socket(address,port==-1?80:port);
sender = new BufferedOutputStream(client.getOutputStream());
receiver = new BufferedInputStream(client.getInputStream());
}catch(UnknownHostException u) {
throw u;
}catch(IOException i) {
throw i;
}
}
/* 封锁与Web处事器的毗连 */
protected void closeServer() throws IOException {
if(client==null) return;
try {
client.close(); sender.close(); receiver.close();
}catch(IOException i) {
throw i;
}
client=null; sender=null; receiver=null;
}
protected String getURLFormat(URL target) {
String spec = "http://"+target.getHost();
if(target.getPort()!=-1)
spec+=":"+target.getPort();
return spec+=target.getFile();
}
/* 向Web处事器传送数据 */
protected void sendMessage(String data) throws IOException{
sender.write(data.getBytes(),0,data.length());
sender.flush();
}
/* 吸收来自Web处事器的数据 */
protected void receiveMessage() throws IOException{
byte data[] = new byte[1024];
int count=0;
int word=-1;
// 理会第一行
while( (word=receiver.read())!=-1 ) {
if(word=='\r'||word=='\n') {
word=receiver.read();
if(word=='\n') word=receiver.read();
break;
}
if(count == data.length) data = addCapacity(data);
data[count++]=(byte)word;
}
String message = new String(data,0,count);
int mark = message.indexOf(32);
serverVersion = message.substring(0,mark);
while( mark<message.length() && message.charAt(mark+1)==32 ) mark++;
responseCode = Integer.parseInt(message.substring(mark+1,mark+=4));
responseMessage = message.substring(mark,message.length()).trim();
// 应答状态码和处理惩罚请读者添加
switch(responseCode) {
case 400:
throw new IOException("错误请求");
case 404:
throw new FileNotFoundException( getURLFormat(target) );
case 503:
throw new IOException("处事器不行用" );
}
if(word==-1) throw new ProtocolException("信息吸收异常终止");
int symbol=-1;
count=0;
// 理会元信息
while( word!='\r' && word!='\n' && word>-1) {
if(word=='\t') word=32;
if(count==data.length) data = addCapacity(data);
data[count++] = (byte)word;
parseLine: {
while( (symbol=receiver.read()) >-1 ) {
switch(symbol) {
case '\t':
symbol=32; break;
case '\r':
case '\n':
word = receiver.read();
if( symbol=='\r' && word=='\n') {
word=receiver.read();
if(word=='\r') word=receiver.read();
}
if( word=='\r' || word=='\n' || word>32) break parseLine;
symbol=32; break;
}
if(count==data.length) data = addCapacity(data);
data[count++] = (byte)symbol;
}
word=-1;
}
message = new String(data,0,count);
mark = message.indexOf(':');
String key = null;
if(mark>0) key = message.substring(0,mark);
mark++;
while( mark<message.length() && message.charAt(mark)<=32 ) mark++;
String value = message.substring(mark,message.length() );
header.put(key,value);
count=0;
}
// 得到正文数据
while( (word=receiver.read())!=-1) {
if(count == data.length) data = addCapacity(data);
data[count++] = (byte)word;
}
if(count>0) byteStream = new ByteArrayInputStream(data,0,count);
data=null;
closeServer();
}
public String getResponseMessage() {
return responseMessage;
}
public int getResponseCode() {
return responseCode;
}
public String getServerVersion() {
return serverVersion;
}
public InputStream getInputStream() {
return byteStream;
}
public synchronized String getHeaderKey(int i) {
if(i>=header.size()) return null;
Enumeration enum = header.propertyNames();
String key = null;
for(int j=0; j<=i; j++)
key = (String)enum.nextElement();
return key;
}
public synchronized String getHeaderValue(int i) {
if(i>=header.size()) return null;
return header.getProperty(getHeaderKey(i));
}
public synchronized String getHeaderValue(String key) {
return header.getProperty(key);
}
protected String getBaseHeads() {
String inf = "User-Agent: myselfHttp/1.0\r\n"+
"Accept: www/source; text/html; image/gif; */*\r\n";
return inf;
}
private byte[] addCapacity(byte rece[]){
byte temp[] = new byte[rece.length+1024];
System.arraycopy(rece,0,temp,0,rece.length);
return temp;
}
}

注: 措施中只实现GET、HEAD、POST三种要领。其他几种因不常利用,暂且忽略。

    关键字:

在线提交作业