C# 使用C将XML数据发布到web服务#
我试图将XML数据集发布到我的web服务中,但是当我尝试发送XML时,会出现500个错误,如果我替换postData=“xmlMsg=Test”,web服务会接受它,但在我尝试发送XML时不会 在我的web服务中,我使用C# 使用C将XML数据发布到web服务#,c#,xml,web-services,C#,Xml,Web Services,我试图将XML数据集发布到我的web服务中,但是当我尝试发送XML时,会出现500个错误,如果我替换postData=“xmlMsg=Test”,web服务会接受它,但在我尝试发送XML时不会 在我的web服务中,我使用 [WebMethod] public string HelloWorld(string xmlMsg) {....... 我的应用程序使用 URL = "http://localhost:55310/Service1.asmx/HelloWor
[WebMethod]
public string HelloWorld(string xmlMsg)
{.......
我的应用程序使用
URL = "http://localhost:55310/Service1.asmx/HelloWorld";
method = "POST";
postData = "xmlMsg=<NewDataSet> <People><PersonId>3293</PersonId> </People></NewDataSet>"
public static string WRequest(string URL, string method, string postData)
{
string responseData = "";
try
{
HttpWebRequest hwrequest = (HttpWebRequest)WebRequest.Create(URL);
hwrequest.Timeout = 60000;
hwrequest.Method = method;
hwrequest.KeepAlive = false;
if (hwrequest.Method == "POST")
{
hwrequest.ContentType = "application/x-www-form-urlencoded";
UTF8Encoding encoding = new UTF8Encoding();
byte[] postByteArray = encoding.GetBytes(postData);
hwrequest.ContentLength = postByteArray.Length;
Stream postStream = hwrequest.GetRequestStream();
postStream.Write(postByteArray, 0, postByteArray.Length);
postStream.Close();
}
// Attempt to receive the WebResponse to the WebRequest.
using (HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse())
{
if (hwresponse != null)
{ // If we have valid WebResponse then read it.
using (StreamReader reader = new StreamReader(hwresponse.GetResponseStream()))
{
XPathDocument doc = new XPathDocument(reader);
XPathNavigator xml = doc.CreateNavigator();
responseData = xml.ToString().Trim();
reader.Close();
}
}
hwresponse.Close();
}
}
catch (Exception e)
{
responseData = "An error occurred: " + e.Message;
}
return responseData;
}
URL=”http://localhost:55310/Service1.asmx/HelloWorld";
方法=“POST”;
postData=“xmlMsg=3293”
公共静态字符串查询(字符串URL、字符串方法、字符串postData)
{
字符串响应数据=”;
尝试
{
hwrequest=(HttpWebRequest)WebRequest.Create(URL);
hwrequest.Timeout=60000;
hwrequest.Method=方法;
hwrequest.KeepAlive=false;
if(hwrequest.Method==“POST”)
{
hwrequest.ContentType=“应用程序/x-www-form-urlencoded”;
UTF8Encoding=新的UTF8Encoding();
字节[]postByteArray=encoding.GetBytes(postData);
hwrequest.ContentLength=postByteArray.Length;
Stream postStream=hwrequest.GetRequestStream();
Write(postByteArray,0,postByteArray.Length);
postStream.Close();
}
//尝试接收对WebRequest的WebResponse。
使用(HttpWebResponse hwresponse=(HttpWebResponse)hwrequest.GetResponse())
{
if(hwresponse!=null)
{//如果我们有有效的WebResponse,请阅读它。
使用(StreamReader=newstreamreader(hwresponse.GetResponseStream())
{
XPathDocument doc=新的XPathDocument(读卡器);
XPathNavigator xml=doc.CreateNavigator();
responseData=xml.ToString().Trim();
reader.Close();
}
}
hwresponse.Close();
}
}
捕获(例外e)
{
responseData=“出现错误:”+e.消息;
}
返回响应数据;
}
有人知道我的错误在哪里吗?很明显,它不喜欢xml,但我不知道为什么或者如何绕过它尝试添加到xml的开头为什么你使用
HttpWebRequest
而不是“添加服务引用”(如果你使用的是.NET 2.0或更低版本,则使用“添加Web引用”)?您好,我尝试在XML的开头添加相同的问题下载一个名为fiddler的工具,并使用传入XML的请求生成器创建请求。此外,请尝试调试服务以查看引发异常的位置。尝试将内容类型更改为:text/xml
,并从postData中删除xmlMsg=