Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx" rel="nofollow noreferrer">HttpWebRequest</a> and <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx" rel="nofollow noreferrer">HttpWebResponse</a> classes to achieve this.</p> <p>For example, to POST to an HTML form that has two fields, <code>username</code> and <code>password</code> you would do something like this:</p> <pre><code>NameValueCollection nv = new NameValueCollection(); nv.Add("username", "bob"); nv.Add("password", "password"); string method = "POST"; // or GET string url = "http://www.somesite.com/form.html"; HttpStatusCode httpStatusCode; string response = SendHTTPRequest(nv, method, url, out httpStatusCode); public static string SendHTTPRequest(NameValueCollection data, string method, string url, out HttpStatusCode httpStatusCode) { StringBuilder postData = new StringBuilder(); foreach(string key in data) { postData.Append(key + "=" + data[key] + "&amp;"); } if(method == "GET" &amp;&amp; data.Count &gt; 0) { url += "?" + postData.ToString(); } HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = method; httpWebRequest.Accept = "*/*"; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; if(method == "POST") { using(Stream requestStream = httpWebRequest.GetRequestStream()) { using(MemoryStream ms = new MemoryStream()) { using(BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(Encoding.GetEncoding(1252).GetBytes(postData.ToString())); ms.WriteTo(requestStream); } } } } return GetWebResponse(httpWebRequest, out HttpStatusCode httpStatusCode); } private static string GetWebResponse(HttpWebRequest httpWebRequest, out HttpStatusCode httpStatusCode) { using(HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) { httpStatusCode = httpWebResponse.StatusCode; if(httpStatusCode == HttpStatusCode.OK) { using(Stream responseStream = httpWebResponse.GetResponseStream()) { using(StreamReader responseReader = new StreamReader(responseStream)) { StringBuilder response = new StringBuilder(); char[] read = new Char[256]; int count = responseReader.Read(read, 0, 256); while(count &gt; 0) { response.Append(read, 0, count); count = responseReader.Read(read, 0, 256); } responseReader.Close(); return response.ToString(); } } } return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload