【问题标题】:How to pass POST parameters to ASP.Net web request?如何将 POST 参数传递给 ASP.Net Web 请求?
【发布时间】:2016-07-15 22:39:52
【问题描述】:

我正在尝试使用 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");

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

【问题讨论】:

    标签: asp.net httpwebrequest


    【解决方案1】:

    试试这样...

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

    【讨论】:

    • 嗨。感谢您的回复,一切都很完美,除了我根本没有得到任何回复。页面上没有打印任何内容。
    • 好吧,我可以补充一点,Response.write() 解决了这个问题!
    • @V0R73X:你的意思是“responseData.Write()”吗?
    • @Nesim Razon 谢谢!你的代码对我有用,而 msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx 没有!
    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多