c# http Post 在 webresponse 中没有得到任何内容

发布于 2024-11-08 07:50:45 字数 1191 浏览 0 评论 0原文

这是我的请求和响应代码。

System.IO.MemoryStream xmlStream = null;
HttpWebRequest HttpReq = (HttpWebRequest)WebRequest.Create(url); 
xmlStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(format));

byte[] buf2 = xmlStream.ToArray();
System.Text.UTF8Encoding UTF8Enc = new System.Text.UTF8Encoding();
string s = UTF8Enc.GetString(buf2);
string sPost = "XMLData=" + System.Web.HttpUtility.UrlDecode(s);
byte[] bPostData =  UTF8Enc.GetBytes(sPost);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.ContentType = "application/x-www-form-urlencoded";
HttpReq.Timeout = 30000;  
request.Method = "POST"; 
request.KeepAlive = true; 
using (Stream requestStream = request.GetRequestStream()) 
{
    requestStream.Write(bPostData, 0, bPostData.Length); 
    requestStream.Close(); 
}

string responseString = "";
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
{ 
    responseString = sr.ReadToEnd(); 
}

此代码的任何部分都不会崩溃。 “format”字符串是其中包含 XML 的字符串。最后,当您尝试查看 responseString 中的内容时,它是一个空字符串。我应该看到从 URL 发回给我的 XML。这段代码中是否缺少某些内容?

Here's my code for Request and Response.

System.IO.MemoryStream xmlStream = null;
HttpWebRequest HttpReq = (HttpWebRequest)WebRequest.Create(url); 
xmlStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(format));

byte[] buf2 = xmlStream.ToArray();
System.Text.UTF8Encoding UTF8Enc = new System.Text.UTF8Encoding();
string s = UTF8Enc.GetString(buf2);
string sPost = "XMLData=" + System.Web.HttpUtility.UrlDecode(s);
byte[] bPostData =  UTF8Enc.GetBytes(sPost);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.ContentType = "application/x-www-form-urlencoded";
HttpReq.Timeout = 30000;  
request.Method = "POST"; 
request.KeepAlive = true; 
using (Stream requestStream = request.GetRequestStream()) 
{
    requestStream.Write(bPostData, 0, bPostData.Length); 
    requestStream.Close(); 
}

string responseString = "";
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
{ 
    responseString = sr.ReadToEnd(); 
}

No part of this code crashes. The "format" string is the one with XML in it. By the end when you try to see what's in the responseString, it's an empty string. I am supposed to see the XML sent back to me from the URL. Is there something missing in this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日落 2024-11-15 07:50:45

我建议简化这个混乱的代码:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "XMLData", format }
    };
    byte[] resultBuffer = client.UploadValues(url, values);
    string result = Encoding.UTF8.GetString(resultBuffer);
}

如果您想直接在 POST 正文中上传 XML,则不应使用 application/x-www-form-urlencoded 作为内容类型。您可能应该指定正确的内容类型,如下所示:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "text/xml";
    var data = Encoding.UTF8.GetBytes(format);
    byte[] resultBuffer = client.UploadData(url, data);
    string result = Encoding.UTF8.GetString(resultBuffer);
}

I would recommend a simplification of this messy code:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "XMLData", format }
    };
    byte[] resultBuffer = client.UploadValues(url, values);
    string result = Encoding.UTF8.GetString(resultBuffer);
}

and if you wanted to upload the XML directly in the POST body you shouldn't be using application/x-www-form-urlencoded as content type. You probably should specify the correct content type, like this:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "text/xml";
    var data = Encoding.UTF8.GetBytes(format);
    byte[] resultBuffer = client.UploadData(url, data);
    string result = Encoding.UTF8.GetString(resultBuffer);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文