java 如何在 HTTP post 请求的消息正文中附加 XML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7737610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:12:27  来源:igfitidea点击:

How to attach XML file in message body of HTTP post request?

javaandroid

提问by Sanket Kachhela

How to attach XML file in message body of HTTP post request?

如何在 HTTP post 请求的消息正文中附加 XML 文件?

can any one give example for that kind

任何人都可以举出那种例子吗

回答by Paresh Mayani

Here is a sample code snippet for making HTTP POST request on the desired URL:

下面是一个示例代码片段,用于在所需的 URL 上发出 HTTP POST 请求:

try { 

String myXML = "<? xml version=1.0> <Request> <Elemtnt> <data id=\"1\">E1203</data> <data id=\"2\">E1204</data> </Element> </Request>";

HttpClient httpClient = new DefaultHttpClient(); 
HttpContext localContext = new BasicHttpContext(); 
HttpPost httpPost = new HttpPost("https://www.google.com/calendar/feeds/default/owncalendars/full"); 

List<NameValuePair> dataToPost = new ArrayList<NameValuePair>(); 

dataToPost.add(new BasicNameValuePair("yourxml", myXML)); 

httpPost.setEntity(new UrlEncodedFormEntity(dataToPost)); 

HttpResponse response = httpClient.execute(httpPost, localContext); 

} catch (Exception e) { 
e.printStackTrace(); 
}