Http客戶端程序已集成在Java語言中
是
協議規范
了解TCP/IP協議集通信的讀者知道
包中
I/O流
HTTP協議同其它TCP/IP協議集中的協議一樣
戶端發往服務端的信息格式如下:
請求方法 URL HTTP協議的版本號
提交的元信息
**空行**
實體
請求方法是對這次連接工作的說明
HEAD
過分析元信息
信息的引入使HTTP協議通信更加穩妥可靠
將上述報文發往Web服務器
HTTP協議的版本號 應答狀態碼 應答狀態碼說明
接收的元信息
**空行**
實體
以上報文發向客戶端
下面用最常用的GET方法
GET HTTP/
accept: www/source; text/html; image/gif; image/jpeg; */*
User_Agent: myAgent
**空行**
這個報文是向主機請求一個缺省HTML文檔
號是
隔
HTTP/
Date: Tue
Server: Apache/
Connection: close
Content
**空行**
HTTP/
是對應答狀態碼的解釋
信息的解釋請參閱Inetrnet標准草案:RFC
import
import java
import java
import java
public class Http {
protected Socket client;
protected BufferedOutputStream sender;
protected BufferedInputStream receiver;
protected ByteArrayInputStream byteStream;
protected URL target;
private int responseCode=
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
String cmd =
+ getBaseHeads()+
sendMessage(cmd);
receiveMessage();
}catch(ProtocolException p) {
p
return;
}catch(UnknownHostException e) {
e
return;
}catch(IOException i)
i
return;
}
}
/*
* HEAD方法只請求URL的元信息
* 文件相同
*/
public void HEAD(String url) {
try {
checkHTTP(url);
openServer(target
String cmd =
+getBaseHeads()+
sendMessage(cmd);
receiveMessage();
}catch(ProtocolException p) {
p
return;
}catch(UnknownHostException e) {
e
return;
}catch(IOException i)
i
return;
}
}
/*
* POST方法是向服務器傳送數據
* 提交表格
*/
public void POST(String url
try {
checkHTTP(url);
openServer(target
String cmd =
HTTP/
cmd +=
cmd +=
cmd += content+
sendMessage(cmd);
receiveMessage();
}catch(ProtocolException p) {
p
return;
}catch(UnknownHostException e) {
e
return;
}catch(IOException i)
i
return;
}
}
protected void checkHTTP(String url) throws ProtocolException {
try {
URL target = new URL(url);
if(target==null || !target
throw new ProtocolException(
this
}catch(MalformedURLException m) {
throw new ProtocolException(
}
}
/*
* 與Web服務器連接
* 異常
*/
protected void openServer(String host
UnknownHostException
header
responseMessage=
try {
if(client!=null) closeServer();
if(byteStream != null) {
byteStream
}
InetAddress address = InetAddress
client = new Socket(address
sender = new BufferedOutputStream(client
receiver = new BufferedInputStream(client
}catch(UnknownHostException u) {
throw u;
}catch(IOException i) {
throw i;
}
}
/* 關閉與Web服務器的連接 */
protected void closeServer() throws IOException {
if(client==null) return;
try {
client
}catch(IOException i) {
throw i;
}
client=null; sender=null; receiver=null;
}
protected String getURLFormat(URL target) {
String spec = //
if(target
spec+=
return spec+=target
}
/* 向Web服務器傳送數據 */
protected void sendMessage(String data) throws IOException{
sender
sender
}
/* 接收來自Web服務器的數據 */
protected void receiveMessage() throws IOException{
byte data[] = new byte[
int count=
int word=
// 解析第一行
while( (word=receiver
if(word==
word=receiver
if(word==
break;
}
if(count == data
data[count++]=(byte)word;
}
String message = new String(data
int mark = message
serverVersion = message
while( mark
responseMessage = message.substring(mark,message.length()).trim();
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25660.html