C#中的multipart / form-data的编程方式提交 [英] c# multipart/form-data submit programmatically

查看:241
本文介绍了C#中的multipart / form-data的编程方式提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,有一个小问题。林创建一个小的应用程序在一个网站上自动执行表单提交。但坏的是,他们正在使用的multipart / form-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.

当然,做这样的失败。


串postData1 =firstfield =+ firststring +secondfield =+ secondstring;

所以我的问题是如何地狱后与多部分形式的表单字段?

So my question is how the hell post those form fields with multipart form?

发布像PHP数组是这样的:

Posting like arrays in php like this:


$ POSTDATA =阵列('firstfield'=> $ firststring,'secondfield'=> $ secondstring);

作品,并传递形式,但似乎没有用C#

works and passes the form but seems not working with c#

有什么建议?

数据提交经过3页(基本屏幕抓取)登录/第一部分/第2部分

Data submission goes through 3 page ( basic screenscrape ) login/part1/part2

到目前为止,我可以成功登录并发布第1部分(使用正常的应用程序/ x-WWW窗体-urlencoded形式)

So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )

但是,当生病尝试后多部分的形式,它失败并送我回到第1部分。因此,也许是我的代码错误的,但在这里它是:

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("\r\n" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"title\"" + "\r\n");
sb.Append("\r\n");
sb.Append(title);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"tags\"" + "\r\n");
sb.Append("\r\n");
sb.Append(tags);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"bodytext\"" + "\r\n");
sb.Append("\r\n");
sb.Append(text);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"summarycheckbox\"" + "\r\n");
sb.Append("\r\n");
sb.Append("on");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"summarytext\"" + "\r\n");
sb.Append("\r\n");
sb.Append(summary);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"remLen\"" + "\r\n");
sb.Append("\r\n");
sb.Append("125");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"category\"" + "\r\n");
sb.Append("\r\n");
sb.Append("1");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"trackback\"" + "\r\n");
sb.Append("\r\n");
sb.Append("");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"url\"" + "\r\n");
sb.Append("\r\n");
sb.Append(link);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"phase\"" + "\r\n");
sb.Append("\r\n");
sb.Append("2");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"randkey\"" + "\r\n");
sb.Append("\r\n");
sb.Append(key2);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"id\"" + "\r\n");
sb.Append("\r\n");
sb.Append(id2);
sb.Append("\r\n--" + boundary + "--" + "\r\n");

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

推荐答案

帖子OS的multipart / form-data的类型有不同的结构,因为他们是为了传输数据,而不仅仅是纯文本。

posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.

下面的格式为:

--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"

[actual value]
--[random number, a GUID is good here]--

使用HttpWebRequest的,您可以创建一个具有这种格式的请求。下面是一个示例:

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

这篇关于C#中的multipart / form-data的编程方式提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆