HttpWebRequest 只发送 Get 请求,不发送 POST
本文关键字:POST Get HttpWebRequest 请求 | 更新日期: 2023-09-27 18:37:22
im 尝试使用 HttpWebRequest 发送 POST 请求,小提琴手向我显示我正在发送 GET?任何帮助将不胜感激,因为我能够看到我做错了什么。
法典:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LOGIN_API_BASE_URL);
string postString = string.Format("api_email={0}&api_password={1}", EMAIL, PASSWORD);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postString);
CookieContainer cookies = new CookieContainer();
request.CookieContainer = cookies;
//request.AllowWriteStreamBuffering = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.Timeout = i_timeout;
//request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//request.Referer = "https://accounts.craigslist.org";
using (Stream writer = request.GetRequestStream())
{
writer.Write(data, 0, data.Length);
//writer.Close();
}
StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
request.GetResponse().Close();
好的,发现了问题,我向其发送请求的 URL http://domain.com,当我将其发送到 http://www.domain.com 时,它可以工作并发送帖子。 所以新问题为什么会这样?(答案会得到我问题的答案,因为我不喜欢投票的想法)