I am developing a simple http client, but i have problem here:
When using telnet on Windows command line :
telnet download.microsoft.com 80
HEAD / HTTP/1.1
Host: download.microsoft.com
Response: 301 Moved Permanently
HEAD /download/7/A/9/7A9EBB22-8DC8-40A2-802D-B4CC343A928E/proplussp2013-kb2817430-fullfile-x86-vi-vn.exe HTTP/1.1
Host: download.microsoft.com
Response: 200 OK
When using my program (Eclipse IDE, JRE8) This is my Java code:
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();
}
}
With getHeader("/")
it run about 20s then response HTTP/1.0 408 Request Time-out
With getHeader("/download/7/A/9/7A9EBB22-8DC8-40A2-802D-B4CC343A928E/proplussp2013-kb2817430-fullfile-x86-vi-vn.exe")
it also get HTTP/1.0 408 Request Time-out
My code work Ok with another host. I also know that host download.microsoft.com is not normal accessible, but why with 2 method above it give different result?
Thanks!
Dump that contents of httpRequest to the screen and you'll see that you're building a broken request (CRLF instead of SP before http-version).
Furhtermore you're missing an additional CRLF (see http://greenbytes.de/tech/webdav/rfc7230.html#http.message )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.