JAVA中带有JSON字符串的HTTP POST请求无效

时间:2014-01-23 10:51:29

标签: java json web-services http http-post

我有以下小代码来获取服务提供商的json回复,我试过的是发布一个http post请求但总是抛出

java.net.UnknownHostException: directory.qantasloyalty.com
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:382)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:509)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:278)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:335)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:176)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:769)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:162)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:861)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
    at Test.Httptestpost.sendPost(Httptestpost.java:124)
    at Test.Httptestpost.main(Httptestpost.java:32)

请在下面找到我的代码,

private void sendPost() {
    System.getProperties().put("http.proxySet", "true");
    System.getProperties().put("http.proxyHost", "proxyurl");
    System.getProperties().put("http.proxyPort", "8080");
    System.getProperties().put("http.proxyUser", "username");
    System.getProperties().put("http.proxyPassword", "pwd");
    System.getProperties().put("http.nonProxyHosts", "localhost|127.0.0.1");

    String url = "httpsurlhere";
    URL obj = null;
    try {
        obj = new URL(url);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpsURLConnection con = null;
    try {
        con = (HttpsURLConnection) obj.openConnection();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //add reuqest header
    try {
        con.setRequestMethod("POST");
    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //con.setRequestProperty("User-Agent", USER_AGENT);
    //con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setRequestProperty("Content-Type", "application/json;UTF-8");

    String urlParameters = "username=demouser&password=aDemoPassword";
    String input = "{\"username\":\"demouser\",\"password\":\"aDemoPassword\"}";
    System.out.println("input"+input);
    System.out.println("url"+con.getURL());
    System.out.println("req prop :"+con.getRequestProperties());
    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr;
    try {
        wr = new DataOutputStream(con.getOutputStream());

        wr.writeBytes(input);

    wr.flush();
    wr.close();


    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

有人可以帮我解决问题

出于安全考虑,我还没有在这里分享确切的代理URL和http post webservice URl

1 个答案:

答案 0 :(得分:0)

请仔细阅读以下示例代码: - public static String handlePostRequest(String X,String Y)             抛出IOException {

    URL url = new URL(X);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Content-Length",
            String.valueOf(Y.length()));

    // Write data
    OutputStream os = connection.getOutputStream();
    os.write(Y.getBytes());

    // Read response
    String responseSB = "";
    BufferedReader br = new BufferedReader(new InputStreamReader(
            connection.getInputStream()));

    String line;
    while ((line = br.readLine()) != null)
        responseSB += line;

    // Close streams
    br.close();
    os.close();
    return responseSB;

}