【问题标题】:Send Xml string through POST method in java在java中通过POST方法发送Xml字符串
【发布时间】:2014-07-08 18:07:21
【问题描述】:

我想通过 POST 方法将 xml 字符串传递给 URL。

我在 sn-p 下面试过了,但它没有返回任何东西

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

但是当我尝试通过 jsp 发布它时,我会从 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"> 

【问题讨论】:

    标签: java post https httpconnection


    【解决方案1】:

    作为参数发送:使用 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());
    

    【讨论】:

    • 难道没有比这种丑陋的字符串化方法更优雅的方法吗?就像 JSON 的美妙工作方式一样。
    【解决方案2】:

    如果你想通过 HTTP POST 以 text/xml 形式发送 XML,你应该使用 OutputStreamWriter:

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

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      相关资源
      最近更新 更多