如何通过POST参数ASP.Net Web请求? [英] How to pass POST parameters to ASP.Net web request?

查看:554
本文介绍了如何通过POST参数ASP.Net Web请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使Web请求编程在ASP.NET,使用POST方法。

我想与Web请求发送POST参数以及。事情是这样的:

I'm trying to make web requests programmatically in ASP.NET, using the POST method.
I'd like to send POST parameters with the web request as well. Something like this:

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");

显然是注释行不起作用。我如何做到这一点?

obviously the commented line does not work. How do I achieve this?

推荐答案

尝试像这样...

  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();

这篇关于如何通过POST参数ASP.Net Web请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆