Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I needed to simulate a browser login to a website to get a login cookie, and the login form was multipart/form-data.</p> <p>I took some clues from the other answers here, and then tried to get my own scenario working. It took a bit of frustrating trial and error before it worked right, but here is the code:</p> <pre><code> public static class WebHelpers { /// &lt;summary&gt; /// Post the data as a multipart form /// &lt;/summary&gt; public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary&lt;string, string&gt; values) { string formDataBoundary = "---------------------------" + WebHelpers.RandomHexDigits(12); string contentType = "multipart/form-data; boundary=" + formDataBoundary; string formData = WebHelpers.MakeMultipartForm(values, formDataBoundary); return WebHelpers.PostForm(postUrl, userAgent, contentType, formData); } /// &lt;summary&gt; /// Post a form /// &lt;/summary&gt; public static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, string formData) { HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest; if (request == null) { throw new NullReferenceException("request is not a http request"); } // Add these, as we're doing a POST request.Method = "POST"; request.ContentType = contentType; request.UserAgent = userAgent; request.CookieContainer = new CookieContainer(); // We need to count how many bytes we're sending. byte[] postBytes = Encoding.UTF8.GetBytes(formData); request.ContentLength = postBytes.Length; using (Stream requestStream = request.GetRequestStream()) { // Push it out there requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); } return request.GetResponse() as HttpWebResponse; } /// &lt;summary&gt; /// Generate random hex digits /// &lt;/summary&gt; public static string RandomHexDigits(int count) { Random random = new Random(); StringBuilder result = new StringBuilder(); for (int i = 0; i &lt; count; i++) { int digit = random.Next(16); result.AppendFormat("{0:x}", digit); } return result.ToString(); } /// &lt;summary&gt; /// Turn the key and value pairs into a multipart form /// &lt;/summary&gt; private static string MakeMultipartForm(Dictionary&lt;string, string&gt; values, string boundary) { StringBuilder sb = new StringBuilder(); foreach (var pair in values) { sb.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, pair.Key, pair.Value); } sb.AppendFormat("--{0}--\r\n", boundary); return sb.ToString(); } } } </code></pre> <p>It doesn't handle file data, just form since that's all that I needed. I called like this:</p> <pre><code> try { using (HttpWebResponse response = WebHelpers.MultipartFormDataPost(postUrl, UserAgentString, this.loginForm)) { if (response != null) { Cookie loginCookie = response.Cookies["logincookie"]; ..... </code></pre>
    singulars
    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. 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