HttpURLConnection POST, conn.getOutputStream() throwing Exception

前端 未结 2 1748
感情败类
感情败类 2021-01-14 10:56

I want to make a POST by using HttpURLConnection. I am trying this in 2 ways, but I always get an excetion when doing: conn.getOutputStream();

The excep

相关标签:
2条回答
  • 2021-01-14 11:13

    The URL simply cannot be reached. Either the URL is wrong, or the DNS server couldn't resolve the hostname. Try a simple connect with a well-known URL to exclude one and other, e.g.

    InputStream response = new URL("http://stackoverflow.com").openStream();
    // Consume response.
    

    Update as per the comments, you're required to use a proxy server for HTTP connections. You need to configure that in the Java side as well. Add the following lines before any attempt to connect to an URL.

    System.setProperty("http.proxyHost", "proxy.example.com");
    System.setProperty("http.proxyPort", "8080");
    

    It suffices to do this only once during runtime.

    See also:

    • Java guides - Networking and proxies
    0 讨论(0)
  • 2021-01-14 11:20

    Without establishing the connection (which in this case requires 1 more step to be performed ie connect), transfer is not possible. connect() should be called after the connection is configured (ie after being done with the set***() on the connection).

    What is missing is:

    conn.connect();
    
    0 讨论(0)
提交回复
热议问题