JAVA:HTTP POST 请求 - 异常



考虑以下端点

https://testapi/test/ap

我正在尝试使用以下标头参数进行 POST 请求

Content-Type : "application/json"
key: "<<TESTKEY>>"

并试图在身体部位的细节下

   {
      "Type": "Y",
      "no": "3185195"
    }

我试过这段代码

URL url = new URL("https://testapi/test/ap");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("key", "testkey");
String postJsonData = "{rn" + 
        "  "Type": "Y",rn" + 
        "  "no": "3185195"rn" +       
        "}";
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postJsonData);
wr.flush();
wr.close();

当我尝试运行此代码时,我遇到以下异常(con.getOutputStream(((

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162)

此外,当我调试此代码时,即使我使用 setRequestMethod("POST"( 将其设置为 POST,我也会在 HttpURLConnection 对象中将方法作为"GET"。

任何帮助将不胜感激。

API 中是否有公开 url 的安全管理器?如果是这样:对象连接正在等待:

  • "连接"套接字权限到目标 URL 的主机/端口组合或
  • 允许此请求的 URLPermission。

来源 : https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html

亲切

相关内容

  • 没有找到相关文章

最新更新