• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

URLConnection getHeaderField problem

Normally, the header returned by URLConnection.getHeaderField(0) will return the http response code:
HTTP/1.0 200 OK
 
The response from this site:
http://satc.gsfc.nasa.gov/
is unknown/unknown
 
When I checked the first lines of the header using a socket, I found that the server actually returned:
HTTP 200 Document follows
 
I guess the lack of a version is confusing java. Is this a valid response ? Does the server have to include a version, or is there a problem in whatever URLConnection uses to parse the HTTP response ?
0
cwineman
Asked:
cwineman
  • 3
  • 2
1 Solution
 
vladi21Commented:
HTTP Trace Applet
http://www.idssoftware.com/demos/trace.html

HEAD / HTTP/1.0
Accept: */*
Connection: Keep-Alive
Host: satc.gsfc.nasa.gov:80


HTTP/1.0 200 OK
Date: Wed, 02 Feb 2000 03:06:57 GMT
Server: NCSA/1.5.2
Last-Modified: Thu, 06 Jan 2000 17:44:19 GMT
Content-Type: text/html
Content-Length: 4804
Age: 16
Connection: keep-alive



----
also look
javafaq:
  (Sect. 15) How do I get a URLConnection to work through proxy firewalls? I.e.
  How do you get your application to do its web accesses through a proxy?
   This is typically needed for any net access to another domain. Tell the run
  time system what you are trying to do, by using these commandline arguments
  when you start the program.

java -DproxySet=true -DproxyHost=SOMEHOST -DproxyPort=SOMENUM  classname

  Note proxyPort is optional and it defaults to 80. Without this, you will see
  an exception like java.net.UnknownHostException or
  java.net.NoRouteToHostException
  The proxy settings work for both java.net.URLConnection, and for
  java.net.Sockets.
  Netscape's and IE's JVMs (at least in versions 4.x+) take the proxy settings
  for applets from the browser's proxy configuration. You can also do URL
  proxies in applications (not applets) with the following code
 
    // set up to use proxy
    System.getProperties().put("proxySet", "true");
    System.getProperties().put("proxyHost", "myproxy.server.name");
    System.getProperties().put("proxyPort", "80");
     
  But how do I know the name of the proxy server?
  This code just tells you how you can get a URL connection to the outside.
  Since it is your proxy server, you are expected to know the name of it. There
  isn't any code that you can write that will allow arbitrary URL connections to
  be initiated from outside the firewall. Think about it! If there were, the
  firewall would not be doing its job.
  Also note there are corresponding socksProxyPort and socksProxyHost for when
  socks is used instead of proxy. The default socks port is 1080.


Netscape FAQ
3.1: How do I use URLConnection through a proxy?
You may be getting the error:  #Security Exception: socket.connect:my.host.net->my.host.net. when using a proxy through a firewall, especially when specifying the CODEBASE property of the APPLET tag.  Try using the IP address as the CODEBASE specification instead of your domain name. 4.0 will handle proxies transparently.  [4.0]

Java Tip 42: Write Java apps that work with proxy-based firewalls
http://www.javaworld.com/javaworld/javatips/jw-javatip42.html

CsProxy (a free proxy server)
http://www.cabletron.com/csproxy/ 

Firewall Access in Java Applets
http://www.idssoftware.com/fwapplet.html

HTTPClient
http://www.innovation.ch/java/HTTPClient/
http://www.innovation.ch/java/HTTPClient/urlcon_vs_httpclient.html


Access Cookies from a Java Applet
http://codeguru.developer.com/java/articles/560.shtml


SOCKS in IE4 and jview

Properties p = System.getProperties();
if (System.getProperty("java.vendor").equals("Microsoft Corp.")) {
 p.remove("socksProxyHost");
}
0
 
heyhey_Commented:
>> Does the server have to include

as far a s I remember the server MUST return the HTTP version.
you can get the HTTP protocol definiton from www.faqs.org (check the STD section)
0
 
cwinemanAuthor Commented:
Thanks for the answers.

My real problem is that when I try to open a URLConnection to that site, get the inputStream and read, it doesn't start with the content.
Normally the first bytes read from an html page will be "<html><head>...". On this site, the first things I read are:
HTTP 200 Document follows
.....other headers

I wasn't sure if it was because the server was doing something wrong, or java was screwing up reading the http response. I sounds like its the server according to heyhey.

The headers that vlad21 got are different than the ones that URLConnection gets. I think URLConnection must use a more simple "GET" request.

I'm not sure how to get around this using any standard java code. Looks like I need to find some other http client.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
vladi21Commented:
0
 
vladi21Commented:
0
 
cwinemanAuthor Commented:
vladi21,

Thanks for the info. The link on getting around a proxy server was helpful, but the link to the HTTP client appears to be broken. I am interested in finding another HTTP client if you know the address.

0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now