java 获取post xml,通过Java中的POST方法发送Xml字符串

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.

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

}

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值