2010-01-09 3 views
2

작은 문제가 있습니다. 한 웹 사이트에서 양식 제출을 자동화하는 작은 응용 프로그램을 만들고 있습니다. 하지만 나쁜 점은 그것들이 multipart/form-data를 사용하고 있다는 것입니다. 제출할 텍스트 필드가 업로드되는 파일이 없습니다.프로그래밍 방식으로 C# multipart/form-data 제출

물론 이렇게하는 것은 실패합니다.

string postData1 = "firstfield="+firststring+"secondfield="+secondstring;

그래서 제 질문은 어떻게 다중 양식하는 양식 필드 포스트 도대체?

$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);

일을하고 양식을 통과하지만, C#을

어떤 제안이 작동하지 보인다 :

같은 PHP에서 배열처럼 게시?


데이터 제출은 3 페이지를 통해 이동 (기본 screenscrape) 로그인/PART1/2 부

지금까지 내가 성공적으로 포스트 PART1에 로그인 할 수 있습니다 (사용 정상을 application/x-www-form-urlencode되고 양식)

하지만 멀티 파트 양식 게시를 시도하면 실패하고 나를 part1로 되돌려 보내줍니다. 그래서 어쩌면 내 코드가 잘못하지만 여기있다 :

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

모든 제안들이 아니라 일반 텍스트 데이터를 전송하는 것을 의미하고 있기 때문에

답변

6

게시물 OS의 다중/폼 데이터 유형이 서로 다른 구조를 가지고 오신 것을 환영합니다 .

--[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를 사용 :

여기 형식입니다. 다음은 샘플입니다 :

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

여기에 multipart form posts in C#에 관한 기사가 있습니다. 이 코드는 결국 RestSharp으로 병합되었습니다. 요청을 생성하는 데 사용할 수있는 훌륭한 라이브러리입니다.

관련 문제