2014-05-20 3 views
4

POST 메서드를 통해 xml 문자열을 URL에 전달하려고합니다. 자바의 POST 메서드를 통해 XML 문자열 보내기

나는 다음 시도 니펫을하지만 아무것도

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"> 

답변

11

하는 매개 변수로 보내기 : 당신이 사용해야 당신이 HTTP의 POST 텍스트/XML로 XML을 보내려면 아파치 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()); 
+1

이 추악한 stringify 메서드를하기보다는 이것을하기위한 더 우아한 방법이 있습니까? JSON이 어떻게 아름답게 작동하는지. – therealprashant

0

를 사용 OutputStreamWriter :

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