2

I'm beginning java network programming but I always get Connect Reset exception for the following code:

import java.io.*;
import java.net.*;
import java.util.*;

public class Net {

    public static void main() 
    {

        try
        {
            // this not working ...  URL url = new URL("http://localhost/test.php");

            // not even the following trial
            URL url = new URL("http://www.google.com.gh/");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();


            // Exception is thrown here
            InputStream in = conn.getInputStream();
        }

        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}

All examples I have tried in my learning process have failed. I don't know what is wrong. Help please.

6
  • Is there a proxy defined when you're just browsing on this particular PC? If so you'll ofcourse need to define the proxy in your code as well. See this question Commented Nov 24, 2013 at 10:04
  • no proxy NickDK. It's a direct connection. Commented Nov 24, 2013 at 10:07
  • Works good for me (with a little correction). maybe a permissions issue? Commented Nov 24, 2013 at 10:19
  • probably, but can't guess what that could be. Commented Nov 24, 2013 at 10:27
  • Your code works just fine for me. Indeed looks like some local connectivity problem. Do you get the same result using any publicly accessible url? Commented Nov 24, 2013 at 10:53

1 Answer 1

1

Have you tries setting some additional info?

  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded");

  conn.setRequestProperty("Content-Length", "" + 
           Integer.toString(urlParameters.getBytes().length));
  conn.setRequestProperty("Content-Language", "en-US");  

  conn.setUseCaches (false);
  conn.setDoInput(true);
  conn.setDoOutput(true);


 DataOutputStream wr = new DataOutputStream (
              connection.getOutputStream ());
  wr.writeBytes (urlParameters);
  wr.flush ();
  wr.close ();


 The urlParameters is a URL encoded string.

 String urlParameters =
    "fName=" + URLEncoder.encode("???", "UTF-8") +
    "&lName=" + URLEncoder.encode("???", "UTF-8")
Sign up to request clarification or add additional context in comments.

4 Comments

I had setDoInput, setDoOutput, setUseCaches in other examples but did not work. I include the others and see
Still not working even with the request parameters set. Can't know why.
You need to send a request first, I edited my answer, does this work?
Ok works now. The problem was from my Antivirus. I think it was blocking the connection. I stopped the antivirus software and it works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.