通过java中的POST方法发送Xml字符串

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

我试过下面的代码片段,但它没有返回任何内容

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得到了正确的响应。

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

将其作为参数发送:使用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 urlParameters = new ArrayList(); 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()); 

如果要使用HTTP POST将XML作为text / xml发送,则应使用OutputStreamWriter

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