Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Help with httpwebrequest POST data to https (http://www.go4expert.com/forums/help-httpwebrequest-post-data-https-t20882/)

justJ 6Feb2010 12:17

Help with httpwebrequest POST data to https
 
1 Attachment(s)
My client (real estate company owner) needs a program that he can install on his secretarys computer that will log into a web site to check times. If a certain time (4 days) has elapsed then I have to make a full screen WebBrowser which goes to that website and it will not go away until his secretary does the posts she has to do. That page is craigslist, now I can auto login with the WebBrowser control but I was looking for a more stealthy way about it and because I didn't want the WebBrowser to keep refreshing and eat up memory so I was looking into sending POST data through httpwebrequest / httpwebresponse. Slap me for making it 100 times harder on myself but I'm already in to deep and didn't think it would be this much of a pain to get working and I don't like to get beat.

This is what I have:

well it wouldn't let me paste the code because I got...

The following errors occurred with your submission:
Too many live links/images found in your post content. Please edit your post or contact the administrator.

but if you download the .txt attachment then you can see it.

Attachment 599


All I can get in return is 2 ansi characters. What am I doing wrong?

justJ 6Feb2010 22:41

Re: Help with httpwebrequest POST data to https
 
I knew i should have waited to ask for help, I needed to decompress the gzip stream. Sooooo happpy now!!

justJ 6Feb2010 22:52

Re: Help with httpwebrequest POST data to https
 
couldn't find the Edit button...

"I knew I shouldn't* have asked for help"...

Figured it out this morning

justJ 7Feb2010 02:26

Re: Help with httpwebrequest POST data to https
 
just out of curiosity, I tried to take it further. (My clients app is done btw and he will get it monday) I actually tried to post to craigslist with httpwebreqeust/response. I tackled the daunting task of finding all of the hidden field names/values and the random names given to other non hidden fields and my post string looks like..


-----------------------------8cc755c93940630
Content-Disposition: form-data; name="postingKey"

ecTZogUM4km0OZ9l
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="test"

k4EeW
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwNjIzMCWF8JdisqDKvbaCEArAjjg9 bBOlGgWCzWqdrC5s46N1T3vQSEiAdfg"

U2FsdGVkX182MjMwNjIzMBRUyB2ookEk94PKQo1Xm5XmSiZVEj uAqhYgf6GfjFkT
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwN:jIz.M.C0lAgzH0cu_ViykKEboT .u4Y_sHlSRDS9_xKVoLqFYcieFptb7S_ejw"

This is my title
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U:2FsdGVkX182MjMwNjIzMI2b9JMWF:w:MIOvBrhDf84 1qq0wPVt0SG.jw"

100.00
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwNjIzMNyAWgJxg2F6U561bgcedPnk V49.l2ymU1dzmUo6e5ZVdg3tD5tngSOY"

westside
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="FromEMail"

myEmail@gmail.com
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwNjIzMHK928d5JVewkOa:zPnjduF: gsVY8jl6:DcEuxTJzcQAlrT"

A
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwNjIzMPAtotst_q9PxC3RKRnOno4f qbXZiK0rG-rXzS_HWzqF_efy2zyQIhc"

U2FsdGVkX182MjMwNjIzMIDcFNP1axrTB6TDnoN2A1oradO0XT ZVOgivLRn7M3IS4kqU7zDR90U
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwNjIzMKGsCrxyEiINL90gcQUpoo.0 NR61mHoLJ-Yg-G_:3Vu2QN"

this is my big description!!!
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="image1"

add
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="imgfile1"; filename=""
Content-Type: application/octet-stream


-----------------------------8cc755c93940630
Content-Disposition: form-data; name="image2"

add
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="imgfile2"; filename=""
Content-Type: application/octet-stream


-----------------------------8cc755c93940630
Content-Disposition: form-data; name="image3"

add
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="imgfile3"; filename=""
Content-Type: application/octet-stream


-----------------------------8cc755c93940630
Content-Disposition: form-data; name="image4"

add
-----------------------------8cc755c93940630
Content-Disposition: form-data; name="imgfile4"; filename=""
Content-Type: application/octet-stream


-----------------------------8cc755c93940630
Content-Disposition: form-data; name="U2FsdGVkX182MjMwNjIzMB:RUyB2ookEk9k21dz4PVo_ 7ZmpNhqCsHLvxlI8jdEA6"

Continue
-----------------------------8cc755c93940630--


Now, compared to what Fiddler show when I do an actual post, this is exactly the same. Here is my code to submit the information..

Code:


            string myData = buildPostString(content);
            string url = getURL(content);
            string referer = urlReferer(url);
            ASCIIEncoding encoding = new ASCIIEncoding();
            //HttpWebRequest webReq = new HttpWebRequest(WebRequest.Create(getURL()), HttpWebRequest);
            byte[] buffer = encoding.GetBytes(myData);
            //tried the below buffer too..
            //byte[] buffer = System.Text.Encoding.GetEncoding(1251).GetBytes(myData);
            HttpWebRequest myRequest = WebRequest.Create(url) as HttpWebRequest;
            myRequest.Method = "POST";
            myRequest.KeepAlive = true;
            myRequest.AllowAutoRedirect = true;
            myRequest.CookieContainer = cookie;
            myRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            myRequest.ContentLength = buffer.Length;
            myRequest.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)";
            myRequest.Headers.Set("Accept-Language", "en-us");
            myRequest.Headers.Set("Accept-Encoding", "gzip, deflate");
            myRequest.ProtocolVersion = HttpVersion.Version10;
            myRequest.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, */*";
            myRequest.Referer = url;
            myRequest.KeepAlive = true;
            myRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");

            myRequest.AllowWriteStreamBuffering = true;

            Stream newStream = myRequest.GetRequestStream();

            newStream.Write(buffer, 0, buffer.Length);

            newStream.Close();
            HttpWebResponse webResp = (HttpWebResponse)myRequest.GetResponse();
            myRequest.CookieContainer.Add(webResp.Cookies);
            Stream responseStream = responseStream = webResp.GetResponseStream();
            if (webResp.ContentEncoding.ToLower().Contains("gzip"))
                responseStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
            else if (webResp.ContentEncoding.ToLower().Contains("deflate"))
                responseStream = new System.IO.Compression.DeflateStream(responseStream, System.IO.Compression.CompressionMode.Decompress);

            StreamReader responseReader = new StreamReader(responseStream, Encoding.Default);

            string result = responseReader.ReadToEnd();
           
            webResp.Close();

            return result;

if I capture what my app is doing in fiddler and compare it to an actual manual post, they both have everything similar in the header and POST string. The page i get back in the response (in my app) is the same form that I want to fill out but it is empty. Instead I should get the next page after submitting the form which is "this is how your post will look" with a continue button. What am I doing wrong? I really hope I figure this out before someone else helps me... but don't let that stall you from helping me ;)

justJ 7Feb2010 16:48

Re: Help with httpwebrequest POST data to https
 
well, I did it. I borrowed some code from "well i cant post a link" and it works great. I haven't analyzed their code to see what I was doing wrong but I got it working and all is great!!

http://aspnetupload.com/Upload-File-...-RFC-1867.aspx

is it going to work?

shabbir 7Feb2010 17:52

Re: Help with httpwebrequest POST data to https
 
Made the link clickable.

tahir_123 31Mar2010 10:44

Re: Help with httpwebrequest POST data to https
 
how you find the random name/value of non hidden fields.
actualy i m posting(httpWebReques) data to webpage where the textboxes name are changing whenever i refresh the page.
how i can find the name of those textboxes before posting the data.

justJ 31Mar2010 11:42

Re: Help with httpwebrequest POST data to https
 
You have to scrape the HTML code from the web page, iterate through each <input> tag and apply the proper value to the random name. Probably the hardest part to make because the hidden fields with random names and values have to be dealt with too.

Good luck.

tahir_123 31Mar2010 12:47

Re: Help with httpwebrequest POST data to https
 
i have used to two httpWebRequest
in the first request i scrap the page and take the name/value of hidden and no hidden fields.
and in the second request i post those field names with proper values, but it not work, am i missing something here.....

Thanks for any suggestions.

justJ 31Mar2010 21:37

Re: Help with httpwebrequest POST data to https
 
probably need to fix your WebRequest headers, cookies ect... I little error and it will not work.


All times are GMT +5.5. The time now is 16:58.