1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

multipart/form-data with webrequest not working

Discussion in 'C, C++, C#' started by xpro, Jun 21, 2010.

  1. xpro

    xpro Regular Member

    Joined:
    Jan 21, 2009
    Messages:
    416
    Likes Received:
    16
    Hello

    I've spent hours trying to make this work, here is the code please let me know if you see anything wrong. The response always comes back with empty content.

    Code:
                string boundary = "-----------------------------7da53243d103d2";
    
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://poster.decaptcher.com/");
                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.Method = "POST";
                request.KeepAlive = true;
                request.Headers.Add("Pragma", "no-cache");
                request.Headers.Add("Accept-Encoding", "gzip, deflate");
                request.Headers.Add("Accept-Language", "en-US");
                request.KeepAlive = true;
                request.Host = "poster.decaptcher.com";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
                request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, */*";
    
    
                String data = "" + boundary + "\r\nContent-Disposition: form-data; name=\"function\"\r\n\r\nbalance\r\n" + boundary + "\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nUSERNAME\r\n" + boundary + "\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\nPASSWORD\r\n" + boundary + "--\r\n\r\n";
                byte[] dataByte = System.Text.Encoding.ASCII.GetBytes(data);
    
    
                request.ContentLength = dataByte.Length;
    
                Stream newStream = request.GetRequestStream();
    
                newStream.Write(dataByte, 0, dataByte.Length);
                newStream.Close();
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
                String result = stream.ReadToEnd();
                response.Close();
                stream.Close();
    
                TextWriter tw = new StreamWriter("decaptcheroutput.html");
                tw.WriteLine(result);
                tw.Close();
    
                //Console.WriteLine(result);
                Console.Write(request.Headers);
                Console.Write(data);
              
                Console.WriteLine(response.Headers);
     
  2. SpazzyMcSpazz

    SpazzyMcSpazz Regular Member

    Joined:
    Apr 20, 2009
    Messages:
    261
    Likes Received:
    76
    try something like ngrep or wireshark and see what kind of headers are being sent out
    i doubt someone will just write code for you
    what language is this anyway? Java? try some python or perl, it's a lot easier
    and i don't think you should define your own boundaries like that... shouldn't it be done automatically?
     
    Last edited: Jun 21, 2010
  3. xpro

    xpro Regular Member

    Joined:
    Jan 21, 2009
    Messages:
    416
    Likes Received:
    16
    This is in C#, I used Fiddler and headers that are begin sent looks similar, so I'm not sure why its not working. Here are the headers from Fiddler and my program.

    Fiddler

    From my C# program

     
  4. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    drop the gzip header for the time being, also lose the expect 100 continue header.

    also, when putting together a MIME form, the boundary value in the header is two "--" shorter than the boundary in the form.

    top one is from the header field of your packet log, the bottom one is from the body of the post packet.
    ---------------------------
    -----------------------------

    also, i don't know if this was just an omission, but the packet from your program is missing the POST header and URL
     
    • Thanks Thanks x 1