简体   繁体   English

JAVA中带有JSON字符串的HTTP POST请求不起作用

[英]HTTP POST request with JSON String in JAVA is not working

I have the below small code to get the json reply from service providers, what i have tried is that to post a http post request but it always throws 我有下面的小代码来获取服务提供商的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)

PLease find my code below, 请在下面找到我的代码,

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();
    }

}

Can someone please help me to route the cause 有人可以帮我解决问题的原因

For security purpose I have not shared the exact proxy URL and http post webservice URl also here 为了安全起见,我还没有在此处共享确切的代理URL和http post webservice URl

please go through these sample codes:- public static String handlePostRequest(String X, String Y) throws IOException { 请检查以下示例代码:-公共静态字符串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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM