Java Socket应答与HTTP服务器的关系
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.}
相關文章
-
Java如何通过Socket实现TCP服务端
Socket 通常也称作套接字,Java如何通过Socket实现TCP服务端?下面本站小编带大家一起来看看详细内容,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网! 1 Java Socket简介所谓socket 通常也称作 -
PPT职称考试“Shockwave Flash Object”控件法
导语:Flash动画闪在PowerPoint2003中的四种方法你知道是什么吗?本站小编已经整理出来了,需要学习的小伙伴们一起来看看吧。Flash是大名鼎鼎的美国Macromedia公司推出的一款优秀的矢量动画制作软件,它简单易学,功能强大,能 -
Javascript实例教程如何使用HoTMetal
HoTMetal中使用javascript3.什么样的资源适合于脚本编写当利用HotMetal PRO 6.0编写宏的时候,你可以使用以下几种资源:预定义事件HoTMetaL PRO 6.0定义了几个事件以及特殊的用户定义的宏来处理它们。这些宏应该根基HoTM -
java如何利用java.net.URLConnection发送HTTP请求
如何通过Java发送HTTP请求,通俗点讲,如何通过Java(模拟浏览器)发送HTTP请求。下面是由本站小编为大家整理的java如何利用onnection发送HTTP请求,喜欢的可以收藏一下!了解更多详情资讯,请关注应届毕业生考试网! 运用原生Jav -
The Best Thing That Could Ever Happen少儿双语阅读
Unit 3 The Best Thing That Could Ever Happen单元 3 最棒的事Three old men were sitting around and talking.三个老头坐在一起聊天。The 80-year-old remarked, "The best thing that could happen to me would j -
javasocket服务端技巧
Java的主要工作是通过编程语言来制作互联网页面、制作动态效果以及网站等技术,以下是小编为大家搜索整理的`javasocket服务端技巧,欢迎阅读!更多精彩内容请及时关注我们应届毕业生考试网!如下的GreetingServer 程序是一 -
Java中基于Aspectwerkz的AOP开发
一、AOP编程概览面向对象编程技术进入软件开发的主流对软件的开发方式产生了极大的影响,开发者可以用一组实体以及这些实体之间的关系将系统形象地表示出来,这使得他们能够设计出规模更大、更复杂的系统,开发周期也比以 -
私家车的坏处The Disadvantages of Private Cars英语作文
The Disadvantages of Private CarsAs the development of the world, our country’s economy has become more and more better, people’s life standard has become much better, so people have money to amuse. -
英语阅读:Tobacco Plant to Make Jet Fuel
An airplane maker, an airline and a biofuel company are working together to make fuel from tobacco plant seed oil.The companies are Boeing, South African Airways and SkyNRG. They are using a new tobac -
JavaScript中的style.cssText使用教程分解
很多人用过 r、lay 等直接设置元素的.样式属性,但是 ext 用过的人就不多了。cssText 本质是什么?cssText 的本质就是设置 HTML 元素的 style 属性值。cssText 怎么用?复制代码 代码如下:lementById("d1")ext = "color:re