How to pass POST parameters to ASP.Net web request?

后端 未结 1 1363
离开以前
离开以前 2020-12-13 15:10

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

1条回答
  •  北海茫月
    2020-12-13 15:44

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

    0 讨论(0)
提交回复
热议问题