HTTP 400 Bad Request on Linux host

I am running a simple HTTP client using sockets to get a web page from a server.

It works perfectly on my Windows XP computer, but when I copy the program over to a linux host, the only response I get from the web server is "HTTP/1.1 400 Bad Request".

I tried recompiling the source on the linux host, but to no avail. (I know, shouldn't have to do this with Java)

Any help is much appreciated. Thank you

[430 byte] By [plurcha] at [2007-10-2 7:18:58]
# 1
Post your code
remonvva at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...
# 2

String theReq="GET / HTTP/1.1"+newline+

"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*"+newline+

"Accept-Language: en-us"+newline+

"Accept-Encoding: gzip, deflate"+newline+

"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; iOpus-I-M; .NET CLR 1.1.4322)"+newline+

"Host: www.sun.com"+newline+

"Connection: Keep-Alive"+newline+

"Cache-Control: no-cache"+newline+newline;

BufferedWriter wr=null;

BufferedWriter fwr=null;

BufferedReader rd=null;

Socket socket=null;

String line=null;

String fileName=null;

SSLSocketFactory sfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();

InetAddress addr=null;

try

{

addr=InetAddress.getByName("www.sun.com");

}

catch(UnknownHostException uhe)

{}

SocketAddress sockaddr = new InetSocketAddress(addr, 443);

try {

socket = sfactory.createSocket();

socket.connect(sockaddr, 2000);

socket.setSoTimeout(2000);

wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));

rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));

} catch(IOException e) {

System.out.print(e);

}

wr.write(theReq);

wr.flush();

for(line = rd.readLine();line !=null && line.indexOf("</body>") == -1;line=rd.readLine())

{

System.out.println(line);

}

There is the code for what I am doing... And Yes, I have tried switching the "User-Agent" header to a Firefox/Linux string.

plurcha at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...
# 3
Oh, and also... I am getting the newline by:public final static String newline = System.getProperty("line.separator");
plurcha at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...
# 4
What is the value of object newLineI typically do a out.println("") and that works fine on XP/IE.
watertownjordana at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...
# 5
I thought that the HTTP spec defines new line to be "\r\n" ... and on Windows that really is the line separator while on Unix it's "\n", so on Windows it would work correctly but fail on Linux.newline = "\r\n"
jsalonena at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...
# 6
Yep, that fixed it! thanks.Do you have a link to where that is stated in the HTTP specification? I am just curious...
plurcha at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...
# 7

Did I give the right answer, or was it the \n for unix/linux answer?

By the way, go to ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt

That's the text version of the RFC. Look at page 31, section 4.1

They impose CRLF - which means Carriage Return and Line Feed

Also, see:

"HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all

protocol elements except the entity-body (see appendix 19.3 for

tolerant applications). The end-of-line marker within an entity-body

is defined by its associated media type, as described in section 3.7."

watertownjordana at 2007-7-16 20:54:30 > top of Java-index,Archived Forums,Socket Programming...