当前位置: 技术问答>java相关
请求http页面没有反应?
来源: 互联网 发布时间:2015-06-30
本文导语: 代码如下 //: JabberClient.java import java.net.*; import java.io.*; import java.util.*; public class JabberClient { public static void main(String[] args) throws IOException { String str=""; InetAddress addr =InetAddress.getByName(args[0]);//主机地址 ...
代码如下
//: JabberClient.java
import java.net.*;
import java.io.*;
import java.util.*;
public class JabberClient
{
public static void main(String[] args)
throws IOException
{
String str="";
InetAddress addr =InetAddress.getByName(args[0]);//主机地址
Socket socket =new Socket(addr, 80);//建立一个socket连接
try
{
System.out.println("socket = " + socket);
BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
String ln="GET / HTTP/1.1 rn"+
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */* rn"+
"Accept-Language: zh-cn rn"+
"Accept-Encoding: gzip, deflate rn"+
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; )rn"+
"Host: "+args[0]+ "rn"+ //args[0]为主机地址
"Connection: Keep-Alive rn"+
"rn";//以上定义一个http head
System.out.println(ln);
//读区返回的第一行结果并显示
str=in.readLine();
System.out.print(str);
}
finally
{
System.out.println("closing...");
socket.close();
}
}
}
运行结果:
H:javacode>oldjava JabberClient 202.115.32.32
socket = Socket[addr=teacher.scu.edu.cn/202.115.32.32,port=80,localport=1664]
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; )
Host: 202.115.32.32
Connection: Keep-Alive
null
closing...
H:javacode>
是不是我的http head写错了?
//: JabberClient.java
import java.net.*;
import java.io.*;
import java.util.*;
public class JabberClient
{
public static void main(String[] args)
throws IOException
{
String str="";
InetAddress addr =InetAddress.getByName(args[0]);//主机地址
Socket socket =new Socket(addr, 80);//建立一个socket连接
try
{
System.out.println("socket = " + socket);
BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
String ln="GET / HTTP/1.1 rn"+
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */* rn"+
"Accept-Language: zh-cn rn"+
"Accept-Encoding: gzip, deflate rn"+
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; )rn"+
"Host: "+args[0]+ "rn"+ //args[0]为主机地址
"Connection: Keep-Alive rn"+
"rn";//以上定义一个http head
System.out.println(ln);
//读区返回的第一行结果并显示
str=in.readLine();
System.out.print(str);
}
finally
{
System.out.println("closing...");
socket.close();
}
}
}
运行结果:
H:javacode>oldjava JabberClient 202.115.32.32
socket = Socket[addr=teacher.scu.edu.cn/202.115.32.32,port=80,localport=1664]
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; )
Host: 202.115.32.32
Connection: Keep-Alive
null
closing...
H:javacode>
是不是我的http head写错了?
|
你好像没有把请求发出去呢
outputstream应该把ln写出去
out.write(ln);
out.flush();
然后再读取in的输入流
outputstream应该把ln写出去
out.write(ln);
out.flush();
然后再读取in的输入流