c# multipart/form-data submit programmatically
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for that.
There is no file uploading just some text fields for submission.
Of course doing it like this it fails.
string postData1 = "firstfield="+firststring+"secondfield="+secondstring;
So my question is how the hell post those form fields with multipart form?
Posting like arrays in php like this:
$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);
works and passes the form but seems not working with c#
Any suggestions?
Data submission goes through 3 page ( basic screenscrape ) login/part1/part2
So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )
But when ill try to post multipart form it fails and sends me back to part1. So maybe is my code wrong but here it is:
string password = "password";
string username = "username";
string link = "http://somelink.com/";
string text = "Blah Blah some text here";
string title = "Blah Blah";
string tags1 = title;
string summary = "Blah Blah summary";
string tags = tags1.Replace(" ", ",");
// Set cookie container
CookieContainer cookieJar = new CookieContainer();
string loginData = "username=" + username + "&password=" + password + "&processlogin=1&return=%2Fsubmit.php";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://loginlink.com/login.php");
myRequest.Method = "POST";
myRequest.ServicePoint.Expect100Continue = false;
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = loginData.Length;
myRequest.CookieContainer = cookieJar;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter = new StreamWriter(myRequest.GetRequestStream());
myWriter.Write(loginData);
myWriter.Close();
// Get the response.
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader = new StreamReader(myResponse.GetResponseStream());
// Read the content.
string output = myReader.ReadToEnd();
// Clean up the streams and the response.
myReader.Close();
myResponse.Close();
Match matchkey = Regex.Match(output, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key1 = matchkey.Groups[1].Value;
Match matchid = Regex.Match(output, "type="hidden" name="id" value="([^"]+)"", RegexOptions.IgnoreCase);
string id1 = matchid.Groups[1].Value;
string postData = "url=" + link + "&phase=1&randkey=" + key1 + "&id=" + id1;
HttpWebRequest myRequest2 = (HttpWebRequest)WebRequest.Create("http://submitpage1.com/submit.php");
myRequest2.Method = "POST";
myRequest2.ServicePoint.Expect100Continue = false;
myRequest2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest2.Timeout = 10000;
myRequest2.ContentType = "application/x-www-form-urlencoded";
myRequest2.ContentLength = postData.Length;
myRequest2.CookieContainer = cookieJar;
myRequest2.KeepAlive = true;
myRequest2.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter2 = new StreamWriter(myRequest2.GetRequestStream());
myWriter2.Write(postData);
myWriter2.Close();
// Get the response.
HttpWebResponse myResponse2 = (HttpWebResponse)myRequest2.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader2 = new StreamReader(myResponse2.GetResponseStream());
// Read the content.
string output1 = myReader2.ReadToEnd();
// Clean up the streams and the response.
myReader2.Close();
myResponse2.Close();
Match matchkey1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key2 = matchkey1.Groups[1].Value;
Match matchid1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string id2 = matchid1.Groups[1].Value;
string boundary = "-----------------------------1721856231228";
// Build up the post
StringBuilder sb = new StringBuilder();
sb.Append("rn" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="title"" + "rn");
sb.Append("rn");
sb.Append(title);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="tags"" + "rn");
sb.Append("rn");
sb.Append(tags);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="bodytext"" + "rn");
sb.Append("rn");
sb.Append(text);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarycheckbox"" + "rn");
sb.Append("rn");
sb.Append("on");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarytext"" + "rn");
sb.Append("rn");
sb.Append(summary);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="remLen"" + "rn");
sb.Append("rn");
sb.Append("125");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="category"" + "rn");
sb.Append("rn");
sb.Append("1");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="trackback"" + "rn");
sb.Append("rn");
sb.Append("");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="url"" + "rn");
sb.Append("rn");
sb.Append(link);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="phase"" + "rn");
sb.Append("rn");
sb.Append("2");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="randkey"" + "rn");
sb.Append("rn");
sb.Append(key2);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="id"" + "rn");
sb.Append("rn");
sb.Append(id2);
sb.Append("rn--" + boundary + "--" + "rn");
string postData1 = sb.ToString();
HttpWebRequest myRequest3 = (HttpWebRequest)WebRequest.Create("http://submitpage2.com/submit.php");
myRequest3.Method = "POST";
myRequest3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest3.Timeout = 10000;
myRequest3.ServicePoint.Expect100Continue = false;
myRequest3.Referer = "http://bookmarkindo.com/submit.php";
myRequest3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest3.ContentType = "multipart/form-data; boundary=" + boundary;
myRequest3.ContentLength = postData1.Length;
myRequest3.CookieContainer = cookieJar;
myRequest3.KeepAlive = true;
myRequest3.AllowAutoRedirect = true;
//Write out postdata
StreamWriter myWriter3 = new StreamWriter(myRequest3.GetRequestStream());
myWriter3.Write(postData1);
myWriter3.Close();
// Get the response.
HttpWebResponse myResponse3 = (HttpWebResponse)myRequest3.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader3 = new StreamReader(myResponse3.GetResponseStream());
// Read the content.
string output2 = myReader3.ReadToEnd();
// Clean up the streams and the response.
myReader3.Close();
myResponse3.Close();
All suggestions are welcome
c# multipartform-data
add a comment |
So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for that.
There is no file uploading just some text fields for submission.
Of course doing it like this it fails.
string postData1 = "firstfield="+firststring+"secondfield="+secondstring;
So my question is how the hell post those form fields with multipart form?
Posting like arrays in php like this:
$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);
works and passes the form but seems not working with c#
Any suggestions?
Data submission goes through 3 page ( basic screenscrape ) login/part1/part2
So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )
But when ill try to post multipart form it fails and sends me back to part1. So maybe is my code wrong but here it is:
string password = "password";
string username = "username";
string link = "http://somelink.com/";
string text = "Blah Blah some text here";
string title = "Blah Blah";
string tags1 = title;
string summary = "Blah Blah summary";
string tags = tags1.Replace(" ", ",");
// Set cookie container
CookieContainer cookieJar = new CookieContainer();
string loginData = "username=" + username + "&password=" + password + "&processlogin=1&return=%2Fsubmit.php";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://loginlink.com/login.php");
myRequest.Method = "POST";
myRequest.ServicePoint.Expect100Continue = false;
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = loginData.Length;
myRequest.CookieContainer = cookieJar;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter = new StreamWriter(myRequest.GetRequestStream());
myWriter.Write(loginData);
myWriter.Close();
// Get the response.
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader = new StreamReader(myResponse.GetResponseStream());
// Read the content.
string output = myReader.ReadToEnd();
// Clean up the streams and the response.
myReader.Close();
myResponse.Close();
Match matchkey = Regex.Match(output, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key1 = matchkey.Groups[1].Value;
Match matchid = Regex.Match(output, "type="hidden" name="id" value="([^"]+)"", RegexOptions.IgnoreCase);
string id1 = matchid.Groups[1].Value;
string postData = "url=" + link + "&phase=1&randkey=" + key1 + "&id=" + id1;
HttpWebRequest myRequest2 = (HttpWebRequest)WebRequest.Create("http://submitpage1.com/submit.php");
myRequest2.Method = "POST";
myRequest2.ServicePoint.Expect100Continue = false;
myRequest2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest2.Timeout = 10000;
myRequest2.ContentType = "application/x-www-form-urlencoded";
myRequest2.ContentLength = postData.Length;
myRequest2.CookieContainer = cookieJar;
myRequest2.KeepAlive = true;
myRequest2.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter2 = new StreamWriter(myRequest2.GetRequestStream());
myWriter2.Write(postData);
myWriter2.Close();
// Get the response.
HttpWebResponse myResponse2 = (HttpWebResponse)myRequest2.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader2 = new StreamReader(myResponse2.GetResponseStream());
// Read the content.
string output1 = myReader2.ReadToEnd();
// Clean up the streams and the response.
myReader2.Close();
myResponse2.Close();
Match matchkey1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key2 = matchkey1.Groups[1].Value;
Match matchid1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string id2 = matchid1.Groups[1].Value;
string boundary = "-----------------------------1721856231228";
// Build up the post
StringBuilder sb = new StringBuilder();
sb.Append("rn" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="title"" + "rn");
sb.Append("rn");
sb.Append(title);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="tags"" + "rn");
sb.Append("rn");
sb.Append(tags);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="bodytext"" + "rn");
sb.Append("rn");
sb.Append(text);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarycheckbox"" + "rn");
sb.Append("rn");
sb.Append("on");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarytext"" + "rn");
sb.Append("rn");
sb.Append(summary);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="remLen"" + "rn");
sb.Append("rn");
sb.Append("125");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="category"" + "rn");
sb.Append("rn");
sb.Append("1");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="trackback"" + "rn");
sb.Append("rn");
sb.Append("");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="url"" + "rn");
sb.Append("rn");
sb.Append(link);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="phase"" + "rn");
sb.Append("rn");
sb.Append("2");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="randkey"" + "rn");
sb.Append("rn");
sb.Append(key2);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="id"" + "rn");
sb.Append("rn");
sb.Append(id2);
sb.Append("rn--" + boundary + "--" + "rn");
string postData1 = sb.ToString();
HttpWebRequest myRequest3 = (HttpWebRequest)WebRequest.Create("http://submitpage2.com/submit.php");
myRequest3.Method = "POST";
myRequest3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest3.Timeout = 10000;
myRequest3.ServicePoint.Expect100Continue = false;
myRequest3.Referer = "http://bookmarkindo.com/submit.php";
myRequest3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest3.ContentType = "multipart/form-data; boundary=" + boundary;
myRequest3.ContentLength = postData1.Length;
myRequest3.CookieContainer = cookieJar;
myRequest3.KeepAlive = true;
myRequest3.AllowAutoRedirect = true;
//Write out postdata
StreamWriter myWriter3 = new StreamWriter(myRequest3.GetRequestStream());
myWriter3.Write(postData1);
myWriter3.Close();
// Get the response.
HttpWebResponse myResponse3 = (HttpWebResponse)myRequest3.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader3 = new StreamReader(myResponse3.GetResponseStream());
// Read the content.
string output2 = myReader3.ReadToEnd();
// Clean up the streams and the response.
myReader3.Close();
myResponse3.Close();
All suggestions are welcome
c# multipartform-data
add a comment |
So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for that.
There is no file uploading just some text fields for submission.
Of course doing it like this it fails.
string postData1 = "firstfield="+firststring+"secondfield="+secondstring;
So my question is how the hell post those form fields with multipart form?
Posting like arrays in php like this:
$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);
works and passes the form but seems not working with c#
Any suggestions?
Data submission goes through 3 page ( basic screenscrape ) login/part1/part2
So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )
But when ill try to post multipart form it fails and sends me back to part1. So maybe is my code wrong but here it is:
string password = "password";
string username = "username";
string link = "http://somelink.com/";
string text = "Blah Blah some text here";
string title = "Blah Blah";
string tags1 = title;
string summary = "Blah Blah summary";
string tags = tags1.Replace(" ", ",");
// Set cookie container
CookieContainer cookieJar = new CookieContainer();
string loginData = "username=" + username + "&password=" + password + "&processlogin=1&return=%2Fsubmit.php";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://loginlink.com/login.php");
myRequest.Method = "POST";
myRequest.ServicePoint.Expect100Continue = false;
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = loginData.Length;
myRequest.CookieContainer = cookieJar;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter = new StreamWriter(myRequest.GetRequestStream());
myWriter.Write(loginData);
myWriter.Close();
// Get the response.
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader = new StreamReader(myResponse.GetResponseStream());
// Read the content.
string output = myReader.ReadToEnd();
// Clean up the streams and the response.
myReader.Close();
myResponse.Close();
Match matchkey = Regex.Match(output, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key1 = matchkey.Groups[1].Value;
Match matchid = Regex.Match(output, "type="hidden" name="id" value="([^"]+)"", RegexOptions.IgnoreCase);
string id1 = matchid.Groups[1].Value;
string postData = "url=" + link + "&phase=1&randkey=" + key1 + "&id=" + id1;
HttpWebRequest myRequest2 = (HttpWebRequest)WebRequest.Create("http://submitpage1.com/submit.php");
myRequest2.Method = "POST";
myRequest2.ServicePoint.Expect100Continue = false;
myRequest2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest2.Timeout = 10000;
myRequest2.ContentType = "application/x-www-form-urlencoded";
myRequest2.ContentLength = postData.Length;
myRequest2.CookieContainer = cookieJar;
myRequest2.KeepAlive = true;
myRequest2.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter2 = new StreamWriter(myRequest2.GetRequestStream());
myWriter2.Write(postData);
myWriter2.Close();
// Get the response.
HttpWebResponse myResponse2 = (HttpWebResponse)myRequest2.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader2 = new StreamReader(myResponse2.GetResponseStream());
// Read the content.
string output1 = myReader2.ReadToEnd();
// Clean up the streams and the response.
myReader2.Close();
myResponse2.Close();
Match matchkey1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key2 = matchkey1.Groups[1].Value;
Match matchid1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string id2 = matchid1.Groups[1].Value;
string boundary = "-----------------------------1721856231228";
// Build up the post
StringBuilder sb = new StringBuilder();
sb.Append("rn" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="title"" + "rn");
sb.Append("rn");
sb.Append(title);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="tags"" + "rn");
sb.Append("rn");
sb.Append(tags);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="bodytext"" + "rn");
sb.Append("rn");
sb.Append(text);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarycheckbox"" + "rn");
sb.Append("rn");
sb.Append("on");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarytext"" + "rn");
sb.Append("rn");
sb.Append(summary);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="remLen"" + "rn");
sb.Append("rn");
sb.Append("125");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="category"" + "rn");
sb.Append("rn");
sb.Append("1");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="trackback"" + "rn");
sb.Append("rn");
sb.Append("");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="url"" + "rn");
sb.Append("rn");
sb.Append(link);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="phase"" + "rn");
sb.Append("rn");
sb.Append("2");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="randkey"" + "rn");
sb.Append("rn");
sb.Append(key2);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="id"" + "rn");
sb.Append("rn");
sb.Append(id2);
sb.Append("rn--" + boundary + "--" + "rn");
string postData1 = sb.ToString();
HttpWebRequest myRequest3 = (HttpWebRequest)WebRequest.Create("http://submitpage2.com/submit.php");
myRequest3.Method = "POST";
myRequest3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest3.Timeout = 10000;
myRequest3.ServicePoint.Expect100Continue = false;
myRequest3.Referer = "http://bookmarkindo.com/submit.php";
myRequest3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest3.ContentType = "multipart/form-data; boundary=" + boundary;
myRequest3.ContentLength = postData1.Length;
myRequest3.CookieContainer = cookieJar;
myRequest3.KeepAlive = true;
myRequest3.AllowAutoRedirect = true;
//Write out postdata
StreamWriter myWriter3 = new StreamWriter(myRequest3.GetRequestStream());
myWriter3.Write(postData1);
myWriter3.Close();
// Get the response.
HttpWebResponse myResponse3 = (HttpWebResponse)myRequest3.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader3 = new StreamReader(myResponse3.GetResponseStream());
// Read the content.
string output2 = myReader3.ReadToEnd();
// Clean up the streams and the response.
myReader3.Close();
myResponse3.Close();
All suggestions are welcome
c# multipartform-data
So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for that.
There is no file uploading just some text fields for submission.
Of course doing it like this it fails.
string postData1 = "firstfield="+firststring+"secondfield="+secondstring;
So my question is how the hell post those form fields with multipart form?
Posting like arrays in php like this:
$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);
works and passes the form but seems not working with c#
Any suggestions?
Data submission goes through 3 page ( basic screenscrape ) login/part1/part2
So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )
But when ill try to post multipart form it fails and sends me back to part1. So maybe is my code wrong but here it is:
string password = "password";
string username = "username";
string link = "http://somelink.com/";
string text = "Blah Blah some text here";
string title = "Blah Blah";
string tags1 = title;
string summary = "Blah Blah summary";
string tags = tags1.Replace(" ", ",");
// Set cookie container
CookieContainer cookieJar = new CookieContainer();
string loginData = "username=" + username + "&password=" + password + "&processlogin=1&return=%2Fsubmit.php";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://loginlink.com/login.php");
myRequest.Method = "POST";
myRequest.ServicePoint.Expect100Continue = false;
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = loginData.Length;
myRequest.CookieContainer = cookieJar;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter = new StreamWriter(myRequest.GetRequestStream());
myWriter.Write(loginData);
myWriter.Close();
// Get the response.
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader = new StreamReader(myResponse.GetResponseStream());
// Read the content.
string output = myReader.ReadToEnd();
// Clean up the streams and the response.
myReader.Close();
myResponse.Close();
Match matchkey = Regex.Match(output, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key1 = matchkey.Groups[1].Value;
Match matchid = Regex.Match(output, "type="hidden" name="id" value="([^"]+)"", RegexOptions.IgnoreCase);
string id1 = matchid.Groups[1].Value;
string postData = "url=" + link + "&phase=1&randkey=" + key1 + "&id=" + id1;
HttpWebRequest myRequest2 = (HttpWebRequest)WebRequest.Create("http://submitpage1.com/submit.php");
myRequest2.Method = "POST";
myRequest2.ServicePoint.Expect100Continue = false;
myRequest2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest2.Timeout = 10000;
myRequest2.ContentType = "application/x-www-form-urlencoded";
myRequest2.ContentLength = postData.Length;
myRequest2.CookieContainer = cookieJar;
myRequest2.KeepAlive = true;
myRequest2.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter2 = new StreamWriter(myRequest2.GetRequestStream());
myWriter2.Write(postData);
myWriter2.Close();
// Get the response.
HttpWebResponse myResponse2 = (HttpWebResponse)myRequest2.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader2 = new StreamReader(myResponse2.GetResponseStream());
// Read the content.
string output1 = myReader2.ReadToEnd();
// Clean up the streams and the response.
myReader2.Close();
myResponse2.Close();
Match matchkey1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string key2 = matchkey1.Groups[1].Value;
Match matchid1 = Regex.Match(output1, "type="hidden" name="randkey" value="([^"]+)"", RegexOptions.IgnoreCase);
string id2 = matchid1.Groups[1].Value;
string boundary = "-----------------------------1721856231228";
// Build up the post
StringBuilder sb = new StringBuilder();
sb.Append("rn" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="title"" + "rn");
sb.Append("rn");
sb.Append(title);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="tags"" + "rn");
sb.Append("rn");
sb.Append(tags);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="bodytext"" + "rn");
sb.Append("rn");
sb.Append(text);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarycheckbox"" + "rn");
sb.Append("rn");
sb.Append("on");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="summarytext"" + "rn");
sb.Append("rn");
sb.Append(summary);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="remLen"" + "rn");
sb.Append("rn");
sb.Append("125");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="category"" + "rn");
sb.Append("rn");
sb.Append("1");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="trackback"" + "rn");
sb.Append("rn");
sb.Append("");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="url"" + "rn");
sb.Append("rn");
sb.Append(link);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="phase"" + "rn");
sb.Append("rn");
sb.Append("2");
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="randkey"" + "rn");
sb.Append("rn");
sb.Append(key2);
sb.Append("rn--" + boundary + "rn");
sb.Append("Content-Disposition: form-data; name="id"" + "rn");
sb.Append("rn");
sb.Append(id2);
sb.Append("rn--" + boundary + "--" + "rn");
string postData1 = sb.ToString();
HttpWebRequest myRequest3 = (HttpWebRequest)WebRequest.Create("http://submitpage2.com/submit.php");
myRequest3.Method = "POST";
myRequest3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest3.Timeout = 10000;
myRequest3.ServicePoint.Expect100Continue = false;
myRequest3.Referer = "http://bookmarkindo.com/submit.php";
myRequest3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest3.ContentType = "multipart/form-data; boundary=" + boundary;
myRequest3.ContentLength = postData1.Length;
myRequest3.CookieContainer = cookieJar;
myRequest3.KeepAlive = true;
myRequest3.AllowAutoRedirect = true;
//Write out postdata
StreamWriter myWriter3 = new StreamWriter(myRequest3.GetRequestStream());
myWriter3.Write(postData1);
myWriter3.Close();
// Get the response.
HttpWebResponse myResponse3 = (HttpWebResponse)myRequest3.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader3 = new StreamReader(myResponse3.GetResponseStream());
// Read the content.
string output2 = myReader3.ReadToEnd();
// Clean up the streams and the response.
myReader3.Close();
myResponse3.Close();
All suggestions are welcome
c# multipartform-data
c# multipartform-data
edited May 1 '13 at 18:38
Will
113k46286372
113k46286372
asked Jan 9 '10 at 22:34
KaidoKaido
11114
11114
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
add a comment |
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
add a comment |
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\Users\awasthi\Downloads\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
add a comment |
The format of multipart/form-data
requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2035229%2fc-sharp-multipart-form-data-submit-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
add a comment |
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
add a comment |
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
answered Jan 9 '10 at 22:49
ArielAriel
3,98821623
3,98821623
add a comment |
add a comment |
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
add a comment |
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
add a comment |
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
answered Jan 20 '12 at 17:03
Brian GrinsteadBrian Grinstead
3,00442422
3,00442422
add a comment |
add a comment |
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\Users\awasthi\Downloads\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
add a comment |
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\Users\awasthi\Downloads\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
add a comment |
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\Users\awasthi\Downloads\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\Users\awasthi\Downloads\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
edited Jan 4 at 14:00
Ashok
84332149
84332149
answered Jan 4 at 13:30
Shubham AwasthiShubham Awasthi
211
211
add a comment |
add a comment |
The format of multipart/form-data
requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.
add a comment |
The format of multipart/form-data
requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.
add a comment |
The format of multipart/form-data
requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.
The format of multipart/form-data
requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.
answered Jan 9 '10 at 22:53
Quick Joe SmithQuick Joe Smith
6,10622333
6,10622333
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2035229%2fc-sharp-multipart-form-data-submit-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown