[英]Java Http Client get 408 Request Time-out
我正在開發一個簡單的http客戶端,但是我在這里遇到問題:
在Windows命令行上使用telnet時 :
telnet download.microsoft.com 80
HEAD / HTTP/1.1
Host: download.microsoft.com
響應:301永久移動
HEAD /download/7/A/9/7A9EBB22-8DC8-40A2-802D-B4CC343A928E/proplussp2013-kb2817430-fullfile-x86-vi-vn.exe HTTP/1.1
Host: download.microsoft.com
響應:200 OK
使用我的程序 (Eclipse IDE,JRE8)時,這是我的Java代碼:
import java.io.*;
import java.net.*;
public class Main {
public static DataInputStream in;
public static BufferedWriter out;
public static String host = "download.microsoft.com";
public static int port = 80;
public static Socket soc;
public static String getHeader(String uri) throws IOException{
String httpRequest = "HEAD " + uri + "\r\n"
+ "HTTP/1.1\r\n"
+ "Host:" + host
+ "\r\n";
String httpResponse = "";
int read;
out.write(httpRequest);
out.flush();
while ((read = in.read()) != -1)
httpResponse += (char)read;
return httpResponse;
}
public static void main(String[] args) throws UnknownHostException, IOException{
soc = new Socket(host, port);
in = new DataInputStream(soc.getInputStream());
out= new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
System.out.println(getHeader("/"));
soc.close();
}
}
使用getHeader("/")
它將運行約20秒鍾,然后響應HTTP / 1.0 408請求超時
使用getHeader("/download/7/A/9/7A9EBB22-8DC8-40A2-802D-B4CC343A928E/proplussp2013-kb2817430-fullfile-x86-vi-vn.exe")
它也會獲得HTTP / 1.0 408請求超時
我的代碼可以在其他主機上正常工作。 我也知道主機download.microsoft.com無法正常訪問,但是為什么上面的2種方法會給出不同的結果?
謝謝!
將httpRequest的內容轉儲到屏幕上,您會看到您正在構建一個損壞的請求(在HTTP版本之前使用CRLF而不是SP)。
此外,您還缺少其他CRLF(請參閱http://greenbytes.de/tech/webdav/rfc7230.html#http.message )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.