2
这是我的代码Request and Response。c#http在web响应中没有得到任何东西
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();
}
此代码没有任何部分崩溃。 “格式”字符串是其中包含XML的字符串。最后,当您尝试查看responseString
中的内容时,它是一个空字符串。我应该看到从URL发回给我的XML。这段代码中是否有缺失?
我无法在namevaluecollection中发送。我需要发送XML – lkwewpp 2011-05-20 22:06:23
@lkwewpp,它是我的示例中发送的XML。你试过了吗?如果'format'变量包含XML,则WebClient将使用'application/x-www-form-urlencoded' POST请求并在XMLData字段中发送XML,并负责正确编码它。如果你想在POST主体中发送XML,那么你根本不应该使用'application/x-www-form-urlencoded'。 – 2011-05-20 22:10:19