Name: jn10789 Date: 11/20/98
Currently if you return an error, say 410 No Session,
to a HttpURLConnection and try and call getResponseCode
the first time you get an EOFException... If you call it again
you get the real correct answer, 410. This is kind of annoying.
Notice the second call in the code below:
URLConnection urlConn=null;
URL profileUrl = makeUrl(attr);
urlConn = profileUrl.openConnection();
urlConn.setDoOutput( true );
urlConn.setDoInput( true );
urlConn.setUseCaches( false );
urlConn.setRequestProperty("Cookie", "sid=" + sessionId );
try {
responseCode = ((HttpURLConnection)urlConn).getResponseCode();
} catch (FileNotFoundException ex) {
responseCode = ((HttpURLConnection)urlConn).getResponseCode();
}
Please note that this is related to bugid 4040926, which
seems to suggest that calling getResponseCode will return a
valid response code not a EOFException.
(Review ID: 35747)
======================================================================
Name: ks88420 Date: 09/13/2000
twofish ~ > java -version
java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-b17)
Java HotSpot(TM) Client VM (build 1.3.0rc1-b17, mixed mode)
When an HttpURLConnection is used to connect to a URL that will return a 401
(authorization required) reply, calling getResponseCode() results in a
FileNotFoundException being thrown.
-----------------------------------------------------------------------------
import java.net.*;
import java.io.*;
public class Test2 {
public static void main(String [] argv) {
URLConnection u = null;
HttpURLConnection c = null;
try { u = (new URL(argv[0])).openConnection(); }
catch (MalformedURLException e) { System.out.println("URL "+argv[0]+" is
malformed.");
System.exit(0); }
catch (IOException e) { System.out.println("IOException "+e+" opening
URL "+argv[0]);
System.exit(0); }
c = (HttpURLConnection) u;
try { c.connect(); } catch (IOException e) { System.out.println("Error
connecting; "+e);
System.exit(0); }
int i = 999;
try { i = c.getResponseCode(); } catch (IOException e) {
System.out.println("getResponseCode failed first try; "+e);
}
try { i = c.getResponseCode(); } catch (IOException e) {
System.out.println("getResponseCode failed second try; "+e);
try { System.out.println(u.getContent().toString()); }
catch (Exception e2) { System.out.println("Exception getting
content; "+e2);
System.exit(0);
}
System.exit(0);
}
System.out.println("Response code is "+i);
}
}
----------------------------------------------------------------------------
twofish ~/pcaproto/pca/client > java -cp . Test2
http://www.cs.princeton.edu/csguide/software
getResponseCode failed first try; java.io.FileNotFoundException:
http://www.cs.princeton.edu/csguide/software
getResponseCode failed second try; java.io.FileNotFoundException:
http://www.cs.princeton.edu/csguide/software
Exception getting content; java.io.FileNotFoundException:
http://www.cs.princeton.edu/csguide/software
twofish ~/pcaproto/pca/client >
---------------------------------------------------------------------------
I've tried connecting to a dummy port so that I could see exactly what text the
HttpURLConnection sent to the server. I then manually connected to a server to
make sure that the request was correctly responded to. As you see, it was.
rayban ~/scratch/java > telnet www 80
Trying 128.112.136.11...
Connected to glia.
Escape character is '^]'.
GET /csguide/software HTTP/1.1
User-Agent: Java1.3.0rc1
Host: www.cs.princeton.edu
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
HTTP/1.1 401 Authorization Required
Date: Wed, 13 Sep 2000 19:21:41 GMT
Server: Apache/1.3.12 (Unix) mod_ssl/2.6.6 OpenSSL/0.9.5a PHP/4.0.2
AuthMySQL/2.20
WWW-Authenticate: Basic realm="CS Unix Password"
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
1da
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>401 Authorization Required</TITLE>
</HEAD><BODY>
<H1>Authorization Required</H1>
This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.<P>
<HR>
<ADDRESS>Apache/1.3.12 Server at www.cs.princeton.edu Port 80</ADDRESS>
</BODY></HTML>
0
(Review ID: 109594)
======================================================================
- duplicates
-
JDK-4191185 HttpURLConnection throws a FileNotFound exception if the resultcode is >= 400
-
- Closed
-
-
JDK-4403652 accept headers values different from what i am requesting
-
- Closed
-
- relates to
-
JDK-8024810 java.net.HttpURLConnection.getResponseCode return IOException instead of code
-
- Closed
-