HttpURLConnection POST, conn.getOutputStream() throwing Exception

前端 未结 2 1769
感情败类
感情败类 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

提交回复
热议问题