Send Xml string through POST method in java

北慕城南 提交于 2019-12-10 08:17:18

问题


I want to pass an xml string through POST method to an URL.

I tried below snippet but it doesn't return anything

disableCertificateValidation();
String url = "https://..url";   //https

Properties sysProps = System.getProperties();
sysProps.put("proxySet", "true");
sysProps.put("proxyHost", "1.2.3.4");
sysProps.put("proxyPort", "80");

Authenticator authenticator = new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
        return (new PasswordAuthentication("userid",
                "password".toCharArray()));
    }
};
Authenticator.setDefault(authenticator);


 String xml = ---xml string;            


URL urll;
HttpURLConnection connection = null;
try {
    // Create connection
    urll = new URL(url);
    connection = (HttpURLConnection) urll.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    connection.setRequestProperty("Content-Length", ""
            + Integer.toString(xml.getBytes().length));
    connection.setRequestProperty("Content-Language", "en-US");

    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    // Send request
    DataOutputStream wr = new DataOutputStream(connection
            .getOutputStream());
    wr.writeBytes(xml);
    wr.flush();
    wr.close();

    // Get Response
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();
    System.out.println("response.toString();"+response.toString());


} catch (Exception e) {

    e.printStackTrace();


} finally {

    if (connection != null) {
        connection.disconnect();
    }
}

But when I try to post it through jsp I get the proper response from the url.

<script type="text/javascript">
function set(){
        document.getElementById("eXml").value=---xml string
    document.getElementById("textt").value=document.getElementById("eXml").value;
    alert(document.getElementById("eXml").value);
    document.getElementById("myForm").action="https---" //https url;
        document.getElementById("myForm").submit();
}
</script>
<body>
<form method="POST" id="myForm">
<input type="submit" name="send" onclick="set()">
<input type="text" id="textt" value='test'>
<input type="hidden" name="eXml" id="eXml"> 

回答1:


Send it as parameter: Using Apache HttpClient

    String url = "https://yoururl.com"; 

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("xml", xmlString));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + post.getEntity());
    System.out.println("Response Code : " + 
                                response.getStatusLine().getStatusCode());

    BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());



回答2:


if you want to send XML as text/xml with HTTP POST you should use OutputStreamWriter:

try(OutputStreamWriter osw = new OutputStreamWriter(cnn.getOutputStream)) {
    osw.write(xmlData);
    osw.flush();
}


来源:https://stackoverflow.com/questions/23760477/send-xml-string-through-post-method-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!