C# HTTP Post 代码未获得预期响应
我有一个具有以下结构的示例 htm 文件,它 POST xml 并获取响应 xml。我需要用 C# 做同样的事情。请参阅 html 下面的我的 C# 代码。
<html>
<body>
<table>
<tr><td width=10%> </td><td><h2>API Test Form</h2></td></tr>
<tr><td width=10%> </td><td><h3>Command: get_Details </h3></td></tr>
<form action="https://test.test.com/getDetails" method=POST>
<tr>
<td width=10%> </td>
<td>
<textarea name="xml" rows=15 cols=80>
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<test1>xcvb</test1>
</Request>
</textarea>
</td>
</tr>
<tr><td width=10%> </td><td> </td></tr>
<tr><td width=10%> </td><td><input type="submit" value="Submit Request"></td></tr>
</table>
</form>
</body>
</html>
private static string MSDNHttpPost1()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://test.test.com/getDetails");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
var doc = new XmlDocument();
doc.Load(@"C:\request.xml");
string postData = doc.InnerXml;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "text/xml";
// Set the ContentLength property of the WebRequest.
//request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
C# 代码改编自 MSDN 站点。但响应显示了一条错误消息,基本上表示服务器无法读取 xml 文件。有人建议我在发布时在 xml 之前包含“data=”。但这对反应没有影响。
关于我缺少什么的任何线索吗?
I have a sample htm file with following structure and it POSTs the xml and gets the response xml. I need to do the same with C#. See my C# code below the html.
<html>
<body>
<table>
<tr><td width=10%> </td><td><h2>API Test Form</h2></td></tr>
<tr><td width=10%> </td><td><h3>Command: get_Details </h3></td></tr>
<form action="https://test.test.com/getDetails" method=POST>
<tr>
<td width=10%> </td>
<td>
<textarea name="xml" rows=15 cols=80>
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<test1>xcvb</test1>
</Request>
</textarea>
</td>
</tr>
<tr><td width=10%> </td><td> </td></tr>
<tr><td width=10%> </td><td><input type="submit" value="Submit Request"></td></tr>
</table>
</form>
</body>
</html>
private static string MSDNHttpPost1()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://test.test.com/getDetails");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
var doc = new XmlDocument();
doc.Load(@"C:\request.xml");
string postData = doc.InnerXml;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "text/xml";
// Set the ContentLength property of the WebRequest.
//request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
The C# code was adapted from MSDN site. But the response shows me a message with a error which basically says the server could not read the xml file. I have been suggested to include "data="before the xml while posting. But that makes no difference on the response.
Any clues as to what I am missing?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发布 XML 时正确的内容类型是:
text/xml 内容类型已过时,而且还严重坏了。
The correct content-type when posting XML is:
The text/xml content-type is obsolete, and also seriously broken.
当您加载 XML 文档时,我认为您想要:
When you loaded the XML document, I think you wanted:
首先,我建议您使用 Fiddler 或类似工具,以便您可以准确查看正在发送的内容在通过 Web 浏览器单独执行时所拥有的表单的 POST 请求中。然后实例化 HttpWebRequest 对象并根据您在 Fiddler 中看到的内容设置它的所有属性。
First of all I would suggest you use Fiddler or similar tool so that you can see exactly what is being send in the POST request of the form that you have when you execute it separately trough a web browser. Then instantiate the
HttpWebRequest
object and set all it's properties as per what you saw in Fiddler.正如问题中的 html 中提到的,请求 xml 位于 TextArea 控件内,
我在请求 xml 前面加上了“xml=”前缀,这就达到了目的。
As mentioned in the html in the question, the request xml is housed within a TextArea control
I prefixed the request xml with "xml=" which did the trick.