Click here to Skip to main content
16,012,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hello,
I have a HttpWebRequest POST function that does a HTTP POST, but after I do a post on a site it doesnt load the entire site. If I use same settings for HttpWebRequest just to load the site without doing a post, it loads just fine.
My code is,

byte[] buffer = Encoding.ASCII.GetBytes(post);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Timeout = 20000;
request.UserAgent = gettext(useragent);
request.ProtocolVersion = HttpVersion.Version10;
request.AllowAutoRedirect = true;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = buffer.Length;
Stream PostData = request.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
_referer = WebResp.ResponseUri.ToString();
string vystup = _Answer.ReadToEnd();
html = RemoveBetween("<!--", "-->", vystup);
PostData.Close();
_Answer.Close();
Answer.Close();


Thanks in advance.
Posted
Updated 7-Oct-10 17:48pm
v3

1 solution

Looking over it quickly, it could be because you don't close the PostData stream until after you call request.GetResponse();

try this:

request.Method = "POST";
request.ContentLength = buffer.Length;
Stream PostData = request.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);

// moved this line of code here instead of at the end
PostData.Close();

HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
_referer = WebResp.ResponseUri.ToString();
string vystup = _Answer.ReadToEnd();
html = RemoveBetween("<!--", "-->", vystup);
_Answer.Close();
Answer.Close();
 
Share this answer
 
Comments
Member 4417892 8-Oct-10 5:37am    
I had it there before, but that doesnt work neither.
Im out of ideas, I tried almost everything, but it just wont load the whole html source.
Nathan St 8-Oct-10 5:57am    
Try viewing the http request and response using a tool such as FireBug, that might give you a clue as to what the problem is.
Member 4417892 8-Oct-10 6:17am    
Additional information, the max length of the html that is 7984

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900