如何发送WEB页面请求信息

lickiswallow 2006-04-24 12:49:49
我需要通过程序,将网页请求GET发送给某个网站,从而获得该网站的响应,但是我用下面的程序块却没有得到任何返回信息,也没有报错。
Socket sentRequest = new Socket("www.sina.com",80); //创建到新浪网连接的套接字

OutputStream out = sentRequest.getOutputStream(); //创建输出流
InputStream in = sentRequest.getInputStream(); //创建输入流

PrintStream outStream = new PrintStream(out); //将输出流包装到PrintStream,
// 便于下面的写入请求信息
outStream.println("GET "+"www.sina.com"+" HTTP/1.0");
outStream.println("Host:"+"www.sina.com");
outStream.println("Accept:*/*");
outStream.println("Referer:");
outStream.println();

BufferedReader read=new BufferedReader(new InputStreamReader(in)); //读出
String Line;
while((Line=read.readLine())!=null) //按行读出服务器的响应信息
{
System.out.println(Line);
}
请问有什么问题么?
...全文
206 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolmomo 2006-05-07
  • 打赏
  • 举报
回复
我想知道将www.sina.com换成IP地址行不行?
lickiswallow 2006-05-07
  • 打赏
  • 举报
回复
为什么用GET www.sina.com HTTP/1.0不能正确发送请求而必须只能使用GET / HTTP/1.0才能正确发送呢?

如果我想访问网页上的一个链接,那么也要发送请求,那是不是请求的第一行也必须是
GET / HTTP/1.0呢?
Cnxiaowei 2006-04-24
  • 打赏
  • 举报
回复
outStream.println("GET "+"www.sina.com"+" HTTP/1.0");
如果你想获取sina的主页,写成
outStream.println("GET "+"/"+" HTTP/1.0");
这样
beikexp 2006-04-24
  • 打赏
  • 举报
回复
网络估计有问题,我测试通过,outStream.println("GET "+"/"+" HTTP/1.0");
lickiswallow 2006-04-24
  • 打赏
  • 举报
回复
我按照Cnxiaowei说的方法做了,下面是服务器返回的信息:
HTTP/1.0 400 Bad Request
Server: squid/2.5.STABLE4
Mime-Version: 1.0
Date: Mon, 24 Apr 2006 06:52:10 GMT
Content-Type: text/html
Content-Length: 1189
Expires: Mon, 24 Apr 2006 06:52:10 GMT
X-Squid-Error: ERR_INVALID_URL 0
X-Cache: MISS from xa-193.sina.com.cn
X-Cache-Lookup: NONE from xa-193.sina.com.cn:80
Connection: close

也就是说没有访问成功,页面显示如下:
ERROR
The requested URL could not be retrieved

--------------------------------------------------------------------------------

While trying to retrieve the URL: www.sina.com

The following error was encountered:

Invalid URL
Some aspect of the requested URL is incorrect. Possible problems:

Missing or incorrect access protocol (should be `http://'' or similar)
Missing hostname
Illegal double-escape in the URL-Path
Illegal character in hostname; underscores are not allowed
Your cache administrator is webmaster.



--------------------------------------------------------------------------------

Generated Mon, 24 Apr 2006 06:31:38 GMT by autoinstall.sina.com.cn (squid/2.5.STABLE5)

这里出现了什么问题呢?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧