asp.net - 如何将POST参数传递给ASP.Net Web请求?

标签 asp.net httpwebrequest

我正在尝试使用POST方法在ASP.NET中以编程方式发出Web请求。
我也想通过网络请求发送POST参数。像这样:

WebRequest req = WebRequest.Create("accounts.craigslist.org/login/pstrdr");
    req.Method = "POST";
    req.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    //WebRequest.Parameters.add("areaabb","hou");

显然,注释行不起作用。我该如何实现?

最佳答案

这样尝试...

  string email = "YOUR EMAIL";
  string password = "YOUR PASSWORD";

  string URLAuth = "https://accounts.craigslist.org/login";
  string postString = string.Format("inputEmailHandle={0}&name={1}&inputPassword={2}", email, password);

  const string contentType = "application/x-www-form-urlencoded";
  System.Net.ServicePointManager.Expect100Continue = false;

  CookieContainer cookies = new CookieContainer();
  HttpWebRequest webRequest = WebRequest.Create(URLAuth) as HttpWebRequest;
  webRequest.Method = "POST";
  webRequest.ContentType = contentType;
  webRequest.CookieContainer = cookies;
  webRequest.ContentLength = postString.Length;
  webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
  webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  webRequest.Referer = "https://accounts.craigslist.org";

  StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
  requestWriter.Write(postString);
  requestWriter.Close();

  StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
  string responseData = responseReader.ReadToEnd();

  responseReader.Close();
  webRequest.GetResponse().Close();

关于asp.net - 如何将POST参数传递给ASP.Net Web请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263082/

相关文章:

c# - 无法访问 session asp.net 核心

c# - 通过 asp.net 中的路径获取属性的值

c# - 如何禁用 RadComboBox 的键盘支持?

c# - 在异步 HttpWebResponse 读取期间捕获未处理的 SocketException

.net - 使用 HTTPWebrequest 上传文件(multipart/form-data)

c# - 通过 FTP 代理的 FTP

javascript - 点击按钮刷新页面后打开Modal

asp.net - WCF RESTful JSON Web 服务发布对象的嵌套列表

c# - HttpWebRequest Response Stream 只返回 64k 的数据

c# - 如何克隆 HttpWebRequest?