DataOutputStream os = new DataOutputStream(con.getOutputStream());投掷连接被拒绝例外

时间:2017-09-28 10:49:27

标签: java json post httpurlconnection

我正在尝试将一些数据发布到服务器中。我正在使用网上提供的样本网址。我无法继续前进因为这条线,

DataOutputStream os = new DataOutputStream(con.getOutputStream());,正在抛出Connection Refused Exception。 这是代码......

public static void main(String[] args) throws Exception {
    try {
        URL url = new URL("https://my-json-server.typicode.com/typicode/demo/posts");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("id", 5);

        DataOutputStream os = new DataOutputStream(con.getOutputStream());
        os.writeBytes(jsonObj.toString());
        os.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
        int responseCode = con.getResponseCode();
        String output;
        StringBuffer sb = new StringBuffer();
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            sb.append(output);
            break;
        } 

        System.out.println("Response Code: "+responseCode);
        System.out.println("URL: "+url);
        System.out.println("Parameters: "+jsonObj.toString());
        System.out.println("Output: "+output.toString());

        con.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

以下是例外情况..

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at insertData.main(insertData.java:37)

请帮我解决这个问题。感谢。

1 个答案:

答案 0 :(得分:0)

你需要将你的BufferedReader改为BufferedInputStream,你所看到的“while”循环是以错误的方式构建的,我可以看到,请点击我之前评论中的链接