Java Socket应答与HTTP服务器的关系
- SUN认证
- 关注:7.51K次
Java Socket应答一直伴随着我们的编程生活,在不断的发展中有很多知识需要我们学习。下面我们就先来看看有关Java Socket应答的代码,有点长,但是看下去就会让你豁然开朗。
HTTP/1.1表示这个HTTP服务器是1.1版,200是服务器对客户请求的应答状态码,OK是对应答状态码的解释,之后是这个文档的元信息和文档正文。(相关应答状态码和元信息的解释请参阅Inetrnet标准草案:RFC2616)。
rt .*;
rt .*;
rt erties;
rt eration;
ic class Http {
ected Socket client;
ected BufferedOutputStream sender;
ected BufferedInputStream receiver;
ected ByteArrayInputStream byteStream;
ected URL target;
ate int responseCode=-1;
ate String responseMessage="";
ate String serverVersion="";
ate Properties header = new Properties();
ic Http() { }
ic Http(String url) {
(url) ;
19.}
20./* GET方法根据URL,会请求文件、数据库查询结果、程序运行结果等多种内容 */
ic void GET(String url) {
{
kHTTP(url);
Server(ost(),ort() );
ng cmd = "GET "+ getURLFormat(target) +" HTTP/1.0rn"
26.+ getBaseHeads()+"rn";
Message(cmd);
iveMessage();
29.} catch(ProtocolException p) {
tStackTrace();
rn;
32.} catch(UnknownHostException e) {
tStackTrace();
rn;
35.} catch(IOException i) {
tStackTrace();
rn;
38.}
39.}
40./*
41.* HEAD方法只请求URL的元信息,不包括URL本身。若怀疑本机和服务器上的.
42.* 文件相同,用这个方法检查最快捷有效。
43.*/
ic void HEAD(String url) {
{
kHTTP(url);
Server(ost(),ort() );
ng cmd = "HEAD "+getURLFormat(target)+" HTTP/1.0rn"
49.+getBaseHeads()+"rn";
Message(cmd);
iveMessage();
52.}catch(ProtocolException p) {
tStackTrace();
rn;
55.}catch(UnknownHostException e) {
tStackTrace();
rn;
58.}catch(IOException i) {
tStackTrace();
rn;
61.}
62.}
63./*
64.* POST方法是向服务器传送数据,以便服务器做出相应的处理。例如网页上常用的
65.* 提交表格。
66.*/
ic void POST(String url,String content) {
{
kHTTP(url);
Server(ost(),ort() );
ng cmd = "POST "+ getURLFormat(target) +"HTTP/1.0rn"+getBaseHeads();
+= "Content-type: application/x-www-form-urlencodedrn";
+= "Content-length: " + th() + "rnrn";
+= content+"rn";
Message(cmd);
iveMessage();
77.}catch(ProtocolException p) {
tStackTrace();
rn;
80.}catch(UnknownHostException e) {
tStackTrace();
rn;
83.}catch(IOException i) {
tStackTrace();
rn;
86.}
87.}
ected void checkHTTP(String url) throws ProtocolException {
{
target = new URL(url);
(target==null || !rotocol()perCase()ls("HTTP") )
w new ProtocolException("这不是HTTP协议");
et = target;
94.} catch(MalformedURLException m) {
w new ProtocolException("协议格式错误");
96.}
97.}
98./*
99.* 与Web服务器连接。若找不到Web服务器,InetAddress会引发UnknownHostException
100.* 异常。若Socket连接失败,会引发IOException异常。
101.*/
ected void openServer(String host,int port) throws
ownHostException,IOException {
r();
onseMessage=""; responseCode=-1;
{
(client!=null) closeServer();
(byteStream != null) {
e(); byteStream=null;
110.}
Address address = yName(host);
nt = new Socket(address,port==-1?80:port);
er = new BufferedOutputStream(utputStream());
iver = new BufferedInputStream(nputStream());
115.}catch(UnknownHostException u) {
w u;
117.}catch(IOException i) {
w i;
119.}
120.}
121./* 关闭与Web服务器的连接 */
ected void closeServer() throws IOException {
(client==null) return;
{
e(); e(); e();
126.} catch(IOException i) {
w i;
128.}
nt=null; sender=null; receiver=null;
130.}
ected String getURLFormat(URL target) {
ng spec = "http://
133.+ost();
(ort()!=-1)
+=":"+ort();
rn spec+=ile();
137.}
138./* 向Web服务器传送数据 */
ected void sendMessage(String data) throws IOException{
e(ytes(),0,th());
h();
142.}
143./* 接收来自Web服务器的数据 */
ected void receiveMessage() throws IOException{
data[] = new byte[1024];
count=0;
word=-1;
148.// 解析第一行
e( (word=())!=-1 ) {
(word=='r'||word=='n') {
=();
(word=='n') word=();
k;
154.}
(count == th) data = addCapacity(data);
[count++]=(byte)word;
157.}
ng message = new String(data,0,count);
mark = xOf(32);
erVersion = tring(0,mark);
e( mark
onseCode = eInt(tring(mark+1,mark+=4));
onseMessage = tring(mark,th())();
164.// 应答状态码和处理请读者添加
ch(responseCode) {
400:
w new IOException("错误请求");
404:
w new FileNotFoundException( getURLFormat(target) );
503:
w new IOException("服务器不可用" );
172.}
(word==-1) throw new ProtocolException("信息接收异常终止");
symbol=-1;
t=0;
176.// 解析元信息
e( word!='r' && word!='n' && word>-1) {
(word=='t') word=32;
(count==th) data = addCapacity(data);
[count++] = (byte)word;
eLine: {
e( (symbol=()) >-1 ) {
ch(symbol) {
't':
ol=32; break;
'r':
'n':
= ();
( symbol=='r' && word=='n') {
=();
(word=='r') word=();
192.}
( word=='r' || word=='n' || word>32) break parseLine;
ol=32; break;
195.}
(count==th) data = addCapacity(data);
[count++] = (byte)symbol;
198.}
=-1;
200.}
age = new String(data,0,count);
= xOf(':');
ng key = null;
(mark>0) key = tring(0,mark);
++;
e( mark
ng value = tring(mark,th() );
(key,value);
t=0;
210.}
211.// 获得正文数据
e( (word=())!=-1) {
(count == th) data = addCapacity(data);
[count++] = (byte)word;
215.}
(count>0) byteStream = new ByteArrayInputStream(data,0,count);
=null;
eServer();
219.}
ic String getResponseMessage() {
rn responseMessage;
222.}
ic int getResponseCode() {
rn responseCode;
225.}
ic String getServerVersion() {
rn serverVersion;
228.}
ic InputStream getInputStream() {
rn byteStream;
231.}
ic synchronized String getHeaderKey(int i) {
(i>=()) return null;
eration enum = ertyNames();
ng key = null;
(int j=0; j<=i; j++)
= (String)Element();
rn key;
239.}
ic synchronized String getHeaderValue(int i) {
(i>=()) return null;
rn roperty(getHeaderKey(i));
243.}
ic synchronized String getHeaderValue(String key) {
rn roperty(key);
246.}
ected String getBaseHeads() {
ng inf = "User-Agent: myselfHttp/1.0rn"+
249."Accept: www/source; text/html; image/gif; */*rn";
rn inf;
251.}
ate byte[] addCapacity(byte rece[]){
temp[] = new byte[th+1024];
ycopy(rece,0,temp,0,th);
rn temp;
256.}
ic static void main(String[] args) {
http=new Http();
259.//("
260.);
i;
(i=0; i<50000; i++) {
(" );
(","ratecontd=101&MM_insert=form1 ");
265.}
266.}
267.}
- 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/itrz/sun/lw5rp6.html