java urlconnection doesnt throw sockettimeoutexception - java

I consistently encounter java httpurlconnections, which, even with a setReadTimeOut(), will not throw a sockettimeoutexception when they should, and it is consistently associated with an eventual (after hanging for 2-3 minutes) Premature EOF Exception. I read in a few blogs that this may have to do with reading in using BufferedReader's readLine method,
String x = ""; while((x=bufferedReader.readLine())!=null){}
at the end of a file if there is no new line character at the end of an inputstream. This does not make sense, why doesn't java's setTimeOut function correctly?
URL url=new URL("");
c=(HttpURLConnection)url.openConnection();
c.setReadTimeout(17000);
BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
String s;
while((s=b.readLine())!=null)
{
} ect.
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
Here are the Response Headers (with exact cookie info truncated):
[HTTP/1.1 200 OK]
p3p ["",""]
x-frame-options [SAMEORIGIN]
Date [Tue, 08 May 2012 15:01:40 GMT]
Vary [Accept-Encoding,User-Agent]
Transfer-Encoding [chunked]
Set-cookie [""=""; path=/; domain=""; expires=Tue, 01-Jan-2036 08:00:01 GMT]
Content-Type [text/html; charset=ISO-8859-1]
Server [Server]

See the documentation for setReadTimeout:
Some non-standard implementation of this method ignores the specified timeout.
If you don't think that's the issue, set the timeout to a very low number like 1 and check. You should get a java.net.SocketTimeoutException exception.

Related

Received fatal alert: handshake_failure when loading html from url

I have a code that worked perfectly yesterday and today it throws an Exception:
The code WORKS on all other websites I tried..
The Code:
String line = "", all = "",URL="https://www.website.co.il/";
URL myUrl = null;
BufferedReader in = null;
try {
myUrl = new URL(URL);
in = new BufferedReader(new InputStreamReader(myUrl.openStream(),"UTF-8"));
while ((line = in.readLine()) != null) {
all += line;
all+='\n';
}
} finally {
if (in != null) {
in.close();
}
}
The Exception:
Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
Can anyone see the problem?
could the owners of the website change something that could have make such a difference?
The SSL Labs report for the the site indicates that it only supports the TLS_RSA_WITH_AES_256_CBC_SHA cipher suite which is not supported by Java. If you look at the handshake simulation section of the report, you will see that handshakes simulated for Java 6, 7 and 8 all fail.
Edit: As #Robert has indicated in the comment, installing the Unlimited Strength Policy Files will add support for this cipher suite in Java, enabling your client to communicate with this site.
Did you made a java update?
Java 7 has TLS1 as default and Java 8 has TLS1.2 as default.
You may need to specify manualy the TLS/SSL version.
https://blogs.oracle.com/java-platform-group/entry/diagnosing_tls_ssl_and_https

httpclient throws readtimeout exception after setting SO_TIMEOUT to infinity

I am using httpclient for Client Side to connect Server. I did not set any timeout(readtimeout - SO_TIMEOUT). From the docs i found that if the timeout not specified then it interpreted as infinity(0).
But httpclient throws "java.net.SocketTimeoutException: Read timed out" exception within 20 to 30 seconds after connecting to server on client side. Also note: i am not facing this issue consistently.
What to do to resolve this? Help me.
Stack Trace :
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at java.io.FilterOutputStream.write(Unknown Source)
at org.apache.commons.httpclient.methods.multipart.Part.sendDispositionHeader(Part.java:220)
at org.apache.commons.httpclient.methods.multipart.Part.send(Part.java:308)
at org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java:385)
at org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity.writeRequest(MultipartRequestEntity.java:164)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
Code:
HttpClient client = new HttpClient();
HttpConnectionManager conn = new SimpleHttpConnectionManager(true);
conn.getParams().setConnectionTimeout(25000);
client.setHttpConnectionManager(conn);
PostMethod meth = new PostMethod(url);//url is server url
meth.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
Part p[] = new Part[parts.size()];//parts is an arraylist variable for server process params
p = (Part[])parts.toArray(p);
((PostMethod)meth).setRequestEntity(new MultipartRequestEntity(p,meth.getParams()));
meth.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
HttpMethod method = (HttpMethod)meth;
int status = client.executeMethod(method);
Exception thrown on client executeMethod
Thanks,
Ganesan

EOF exception while sending file using UDP in java?

I was trying code from this link to send file using UDP,
Emulate UDP Over TCP in multithread Server & Client
But i'm getting EOF exception, I don't know why this code is giving such error, i doubled check the code , it seems right, I will be grateful for any help, i need this code for my thesis. Thanks in advance !!!!
SERVER: Accepted connection.
SERVER: receivedput C:\Users\Nika\workspace\test\10MB.txt
THREAD: put C:\Users\Nika\workspace\test\10MB.txt
*** Ready to receive file on port: 60829 ***
*** The file will be saved as: Recieved-10MB.txt ***
*** Expecting to receive: 104857600 bytes ***
Mar 30, 2014 11:17:39 PM udpemu.CLIENTConnection run
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at udpemu.UDPFileReceiver.deserialize(UDPFileReceiver.java:126)
at udpemu.UDPFileReceiver.<init>(UDPFileReceiver.java:52)
at udpemu.CLIENTConnection.run(CLIENTConnection.java:60)
at java.lang.Thread.run(Unknown Source)

Problem with SAX

I'm having the code below..
System.setProperty("http.proxyHost","176.6.129.25") ;
System.setProperty("http.proxyPort", "8080") ;
Authenticator.setDefault(new MyAuthenticator());
//http://deadlock.netbeans.org/hudson/api/xml
*URL url = new URL("http://in8301782d:8080/api/xml");*
Document dom = new SAXReader().read(url);
for( Element job : (List<Element>)dom.getRootElement().elements("job")) {
System.out.println(String.format("Name:%s\tStatus:%s",
job.elementText("name"), job.elementText("color")));
}
This code is working if I replace with http://deadlock.netbeans.org/hudson/api/xml , but it is not working with http://in8301782d:8080/api/xml. Infact If I type the samething in browser it is working.. If I replace the hostname with Ip address also not working.
The exception I'm getting is below.
*With in8301782d (machine name)* :
xception in thread "main" org.dom4j.DocumentException: http://in8301782d:8080/api/xml Nested exception: http://in8301782d:8080/api/xml
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:291)
at com.sg.hudson.ci.Main.main(Main.java:140)
Nested exception:
java.io.FileNotFoundException: http://in8301782d:8080/api/xml
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:291)
at com.sg.hudson.ci.Main.main(Main.java:140)
*With Ip (http://176.6.55.55:8080/api/xml):*
Exception in thread "main" org.dom4j.DocumentException: Server returned HTTP response code: 503 for URL: http://176.6.66.156:8080/api/xml Nested exception: Server returned HTTP response code: 503 for URL: http://176.6.66.156:8080/api/xml
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:291)
at com.sg.hudson.ci.Main.main(Main.java:140)
Nested exception:
java.io.IOException: Server returned HTTP response code: 503 for URL: http://176.6.66.156:8080/api/xml
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:291)
at com.sg.hudson.ci.Main.main(Main.java:140)
This doesn't appear to be a problem with your code.
In the first exception, SAX is telling you that it cannot find http://in8301782d:8080/api/xml - it cannot reach this URL. If the URL works in your browser, then perhaps the proxy setup is at fault.
In the second exception, SAX is reporting that the server is returning a non-success HTTP status code of 503, which (according to the HTTP specification) means:
The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay.
Of course, the application sitting at this URL may be sending you a HTTP 503 for some non-standard reason as well.

UnknownHostException while redirecting queries to google and getting results in JSon object

Loading classifier from D:\PROJECT\classifiers\NERDemo\classifiers\ner-eng-ie.crf-3-all2008.ser.gz ... done [2.0 sec].
Original Query was riot in India.
Parsing Queries and expanding tokens from the Ontologies..
{locations=[India], events=[riot]}
Search query is null
Something went wrong...
java.net.UnknownHostException: ajax.googleapis.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at org.girs2.SearchHandler.makeQuery(SearchHandler.java:35)
at org.girs2.GIRS.search(GIRS.java:37)
at org.girs2.GIRS.main(GIRS.java:62)
Exception in thread "main" java.lang.NullPointerException
at org.girs2.GIRS.search(GIRS.java:44)
at org.girs2.GIRS.main(GIRS.java:62)
Looks like you probably have or had a DNS lookup failure. This tells you what happened:
java.net.UnknownHostException: ajax.googleapis.com
If you always get this exception and you can otherwise access this host by name, then I don't know what this could be, but odds are that this was a temporary DNS failure.
What should you do when you get this Exception? Well, if the address is one that you fully expect you should be able to get to -- such as this one -- then when you catch the Exception, you sleep for a while (perhaps 10 or 15 seconds, maybe even 30 or 60 seconds, depending on your application's needs) and try again.
If after several retries -- don't just retry forever! -- you still cannot reach the site, then log a reasonable complaint or otherwise complain that you cannot reach the site and have your application exit.
There's not much you can do when DNS fails except give up or wait and try again.

Categories