:|
Zacznijmy od początku, koncentrując się na moim przykładzie(który znajdziecie u góry);
A więc tak:
1.) Tworzymy nowy obiekt klasy HttpWebRequest oraz ustawiamy w nim potrzebne właściwości
// Było action="index.php" a więc url = http://serwer.com/index.php
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://serwer.com/index.php");
req.Method = "POST";
req.Accept = @"text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg,
image/gif, image/x-xbitmap, */*;q=0.1";
req.Referer = "http://mail.walla.com"; // Czyżby był to link zwrotny, czyli returnurl ??
req.ContentType = @"multipart/form-data;
boundary=---------------------------7d5158213e047e"; // czy tam boundary może wyglądać?
req.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
req.KeepAlive = true;
req.Headers.Add("Pragma", "no-cache");
req.Headers.Add("Accept-Languag", "en-us");
req.Timeout = Timeout.Infinite;
2.) krokiem drugim będzie utworzenie zmiennej payload:
string payload =
"---------------------------7d5158213e047e\r\n" +
"Content-Disposition:form-data; input1=\"wartosc_inputa1\"\r\n\"" +
"\r\n" +
"---------------------------7d5158213e047e\r\n" +
"Content-Disposition:form-data; input2=\"wartosc_inputa2\"\r\n\"" +
"\r\n" +
"---------------------------7d5158213e047e\r\n" +
"Content-Disposition:form-data; input3=\"sciezka_do_pliku\"\r\n\"" +
"\r\n" +
"Content-Type: " + "plain/text" + "\r\n" + // czy tu aby napewno ma być plain/text jak przesyłam plik?
"\r\n";
FileInfo file = new FileInfo("C:\\plik.txt");
int filesize = Convert.ToInt32(file.Length); // waga naszego pliku
req.ContentLength = payload.Length + file.Length; // i wszystko razem
3.) Utworzenie strumienia z Requesta i wpisanie do niego payload i file:
Stream conn = req.GetRequestStream();
byte[] body_header = System.Text.Encoding.ASCII.GetBytes(payload);
conn.Write(body_header, 0, body_header.Length);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] file_content = new byte[filesize];
int len = fs.Read(file_content, 0, filesize);
conn.Write(file_content, 0, len);
fs.Close();
Stream respon = req.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(respon);
sr.Close();
respon.Close();
conn.Close();
Gdzie popełniłem błąd?
W którym kroku?
A wracając do sniffera:
Oto log:
POST / HTTP/1.1
User-Agent: Opera/9.00 (Windows NT 5.0; U; en)
Host: <tu host>
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: pl,en;q=0.9
Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1