【问题标题】:How to attach XML file in message body of HTTP post request?如何在 HTTP 发布请求的消息正文中附加 XML 文件?
【发布时间】:2011-10-12 19:50:39
【问题描述】:

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

谁能举个例子

【问题讨论】:

标签: java android


【解决方案1】:

这是一个示例代码 sn-p,用于在所需 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(); 
}

【讨论】:

  • 我看到了代码。我在这一行有一个问题 dataToPost.add(new BasicNameValuePair("yourxml", post_your_xml));我在论证中写了什么?
  • @Gensheriff 没错,创建 XML 字符串并在此处传递。
  • 类似 StringBuilder sb=sb.append(""); sb.append("").sb.append("the_command").sb.append(""); sb.append("");我对不对?
  • 如果我在 XML 标记中有属性。比我必须用字符串写什么?
  • @Gensheriff 只需创建整个 XML 的单个字符串。如果您在 XML 中有属性,则使用 "\" 转义字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多