Java uses httpurlconnection request exceptions: Java.net.SocketException:Software caused connection Abort:recv failed

Source: Internet
Author: User
Java.net.SocketException:Software caused connection Abort:recv failed according to the exception information is socketexception, I searched this exception information solution, There is almost no solution to the problem I have encountered, and the main solution is to use a while (true) loop, within the loop, to judge if the current InputStream available () method returns a value greater than 0 to indicate a service-side response. But if equal to zero indicates that the server is not responding, the solution here, I think and not resolved, here just to avoid the exception of the production, that is, only to ensure that the output is not the output of abnormal information, but the output of customized information, but the fundamental problem is still unresolved.



at first, the main focus was on the solution described above, I thought it was a service-side disconnect that caused the InputStream input stream to fail, and I didn't find the problem until midnight. The next morning, I found out that my program was unsuccessful when I opened the connection. Hashcode=-1, which then causes the following program to fail when the stream is fetched, and ultimately to fetch the data when the exception message is thrown: Java.net.SocketException:Software caused connection Abort:recv failed




my reason is that the stitching of good URL string in the middle of the space, especially the date format of the data, in the format will often use YYYY-MM-DD HH:mm:ss such a format, the general middle is to retain a space, this time need to main, the implied space in the inside, At the time of formatting, it is best to fill the space directly into the%20, in the browser, the space will be directly escaped as a%20, but we write this request often ignore the problem, so that the URL itself is wrong, when you use this URL to open the connection, The connection cannot be obtained because of a problem with the URL (the format in the URL is inconsistent with the format specified in the protocol). A series of abnormal problems will occur naturally.


Note: Here, when you have Chinese in your URL, be careful to encode the Chinese part.


The following is part of the code:

            URL realurl = new URL (urlnamestring);
            The connection between open and URL connection = Realurl.openconnection ();
            Connection.setconnecttimeout (30000);
            
            Connection.setreadtimeout (30000);
            Sets the common request attribute Connection.setrequestproperty ("accept", "*/*");
            Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1;
            SV1) ");
            
            Establish the actual connection connection.connect ();
            
            Defines the response of a bufferedreader input stream to read a URL InputStream inputstream = Connection.getinputstream ();
            
            in = new BufferedReader (new InputStreamReader (InputStream));
            String Line;
            	while (true) {if (line = In.readline ())!= null) result + = line;
       else break;     	
            } 

I'm not sure this must be your problem, but it's one of the things that actually happens, and hopefully it will be useful for readers who see this blog.


To sum up:

1. Encounter problems directly debug run, see exactly which step appears in the problem.

2. It is necessary to find the root cause of the bug, as far as possible, but the root cause I think is the ultimate goal.

3. Testing through a variety of ways to facilitate the discovery of existing problems.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.