我正在尝试将一个非常基本的XML字符串从Windows Phone应用程序发送到使用Java-WS(以及Provider实现)运行的Web服务,并检索响应。我可以看到字符串使它(通过服务器上的TCP监视器)看起来正确,但Web服务立即导致异常,说源对象为空。请注意,使用Java作为客户端,我可以获得响应(在TCP监视器中列出相同的请求字符串),尽管我的实现略有不同。以下是我在.NET中的表现:
string req = "<?xml version='1.0' encoding='UTF-8'?><request><requestor>WP7.1</requestor></request>";
string url = "http://mywebservice/endpoint1test";
WebClient wc = new WebClient();
wc.Headers["ContentType"] = "text/xml";
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
wc.UploadStringAsync(new Uri(url), "POST", req);
void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
Debug.WriteLine("Web service says: " + e.Result);
}
结果始终为空字符串,因为服务器在异常时返回null。我觉得我错过了一些简单的东西,比如标题或编码。如果它有用,那么这是客户端的Java版本:
String data = "<?xml version='1.0' encoding='UTF-8'?><request><requestor>WP7.1</requestor></request>";
// Send data
URL url = new URL(testUrl);
java.net.URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","text/xml");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.close();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
答案 0 :(得分:1)
你必须改变这一行:
wc.Headers["Content-Type"] = "text/xml";
你应该注意到你错过了一个破折号=)