我有这样的代码:
var uri = "myURL.com"
var request = (HttpWebRequest)WebRequest.Create(uri);
string postData = "myData";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.UseDefaultCredentials = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.AddRange(1024);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString);
response.Close();
stream.Close();
我得到一个例外:“此流不支持搜索操作”。 stream.Length
和stream.Position
中出现此错误。我虽然认为,由于这个错误,我的postData
没有发送到服务器。
Here is the screenshot of the exception