科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用Socket类实现HTTP协议客户端应用(2)

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Http客户端程序已集成在Java语言中,可以通过URLConnection类调用。遗憾的是,由于SUN没有公布Http客户程序的源码,它实现的细节仍是一个谜。本文根据HTTP协议规范,用Java.net.Socket类实现一个HTTP协议客户端程序。

作者:梁颖健 来源:51cto.com整理  2007年9月3日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

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==´ ´||word==´ ´) {

word=receiver.read();

if(word==´ ´) 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!=´ ´ && word!=´ ´ && word>-1) {

if(word==´ ´) word=32;

if(count==data.length) data = addCapacity(data);

data[count++] = (byte)word;

parseLine: {

while( (symbol=receiver.read()) >-1 ) {

switch(symbol) {

case ´ ´:

symbol=32; break;

case ´ ´:

case ´ ´:

word = receiver.read();

if( symbol==´ ´ && word==´ ´) {

word=receiver.read();

if(word==´ ´) word=receiver.read();

}

if( word==´ ´ || word==´ ´ || 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 "+

"Accept: www/source; text/html; image/gif; */* ";

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三种方法。其他几种因不常使用,暂且忽略。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章