用VS2008编写PDA程序,在调用接口的时候会提示:此请求需要数据缓冲以便成功进行身份验证或重定向.
public string GetHttpWebRequest(string fullUrl, string body)
{
try
{
System.Net.ServicePointManager.DefaultConnectionLimit = 50;
Encoding encoding = Encoding.GetEncoding("utf-8");
string res = string.Empty;
//请求
HttpWebRequest httpWebRequest = null;
Stream postStream = null;
//响应
HttpWebResponse httpwebResponse = null;
StreamReader streamReader = null;
//请求
httpWebRequest = (HttpWebRequest)WebRequest.Create(fullUrl);
httpWebRequest.Proxy = new WebProxy();
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpWebRequest.Timeout = 600000;
//向请求流写数据
byte[] postData = encoding.GetBytes(body);
httpWebRequest.ContentLength = postData.Length;
postStream = httpWebRequest.GetRequestStream();
postStream.Write(postData, 0, postData.Length);
//必须加上以下代码
postStream.Flush();
postStream.Close();
//httpWebRequest.AllowWriteStreamBuffering = true;
//httpWebRequest.PreAuthenticate = true;
//httpWebRequest.SendChunked = true;
//httpWebRequest.AllowAutoRedirect = true;
//httpWebRequest.ProtocolVersion = HttpVersion.Version11;
//身份凭证
//CredentialCache myCredential = new CredentialCache();
//myCredential.Add(new Uri(fullUrl), "Basic", new NetworkCredential("username", "password"));
//httpWebRequest.Credentials = myCredential;
//响应
httpwebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//1、此处在wince平台上报“在写入请求数据前,不能检索此请求的响应”
streamReader = new StreamReader(httpwebResponse.GetResponseStream(), encoding);
res = streamReader.ReadToEnd();
httpwebResponse.Close();
return res;
}
catch (Exception ex)
{
MessageBox.Show(fullUrl + ",访问WebAPI失败,原因是:" + ex.Message);
return "";
}
}
网上看添加HttpWebRequest.AllowWriteStreamBuffering = true,但是添加后又会报其他错
有的说添加request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";但还是报这个错
有点急,麻烦看看