HttpWebRequest Problems Encountered

aronquiray

Member
Joined
Oct 13, 2011
Messages
8
Programming Experience
1-3
Hello again vbforums,
i got a small piece of problem hope you can help me out.

basically. i know how to do the login in different sites using httpwebrequest..

but now, i was working with something like. i am trying to comment to one of my wordpress website,
Dim postData As String = "author=" & txtUser.Text & "&email=" & txtEmail.Text & "&url=" & txtWebsite.Text & "&comment=" & txtMessage.Text & "&submit=&comment_post_ID=" & stringA
        Dim tempcookies As New CookieContainer
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)
        Dim postreq As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.myurl.com/wp-comments-post.php"), HttpWebRequest)
        postreq.Method = "POST"
 
        postreq.KeepAlive = True
        postreq.CookieContainer = tempcookies
        postreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729"
        postreq.ContentType = "application/x-www-form-urlencoded"
        postreq.Referer = "url"
        postreq.ContentLength = byteData.Length
        Dim postreqstream As Stream = postreq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()
        Dim postresponse As HttpWebResponse
        postresponse = DirectCast(postreq.GetResponse, HttpWebResponse)
        tempcookies.Add(postresponse.Cookies)


but i am stuck with the post respone i'm getting an error of.
The remote server returned an error: (500) Internal Server Error.

but when I will try to login in wordpress, with the format of login post data.. it logs me,


hoping for an answer thanks!
 
The code doesn't look overly wrong, but I don't know how it would work with the actual server and its application, "500 Internal Server Error" usually indicate that server app is either faulty at the moment or has been submitted invalid data that it can not handle.
Just a few comments, this code:
Dim postData As String = "author=" & txtUser.Text & "&email=" & txtEmail.Text & "&url=" & txtWebsite.Text & "&comment=" & txtMessage.Text & "&submit=&comment_post_ID=" & stringA
This would be a lot more readable if you used String.Format with parameters:
Dim values() As String = {txtUser.Text, txtEmail.Text, txtWebsite.Text, txtMessage.Text, stringA}
Dim postData As String = String.Format("author={0}&email={1}&url={2}&comment={3}&submit=&comment_post_ID={4}", values)

In that string you can see (now more clearly) that there is no value for submit key, is that valid?
postreq.Referer = "url"
Perhaps this is not used by server, but if it is should it really be the literal string "url" and not an actual url?

Try a web debugger like Fiddler and have a look at the messages sent and received during a successful regular browsing session, maybe you can see what your request is missing compared to that.
 
The code doesn't look overly wrong, but I don't know how it would work with the actual server and its application, "500 Internal Server Error" usually indicate that server app is either faulty at the moment or has been submitted invalid data that it can not handle.
Just a few comments, this code:

This would be a lot more readable if you used String.Format with parameters:
Dim values() As String = {txtUser.Text, txtEmail.Text, txtWebsite.Text, txtMessage.Text, stringA}
Dim postData As String = String.Format("author={0}&email={1}&url={2}&comment={3}&submit=&comment_post_ID={4}", values)

In that string you can see (now more clearly) that there is no value for submit key, is that valid?

Perhaps this is not used by server, but if it is should it really be the literal string "url" and not an actual url?

Try a web debugger like Fiddler and have a look at the messages sent and received during a successful regular browsing session, maybe you can see what your request is missing compared to that.

This is the one you can see on LiveHttpHeaders
'author=a57rf&email=abcdefg%40yahoo.com&url=http%3A%2F%2Fyah.com&comment=xxxxxx+sample&submit=&comment_post_ID=8


'if I substitute the values to my variables like below, it gives me an error 500, Internal Server Error
Dim postData As String = "author=" & txtUser.Text & "&email=" & txtEmail.Text & "&url=" & txtWebsite.Text & "&comment=" & txtMessage.Text & "&submit=&comment_post_ID=" & stringA

i tried to remove the &comment_post_ID , it woudn't give me an error, but it doesnt give me a result also, its like a blank result.


the referURL, is originally a url, i just substitute it temporarily as "url", but its really a url inside

do you have an idea how to tweak this?


Thanks
 
If you reference System.Web library you can use HttpUtility.UrlEncode method to encode your values, for example "abcdefg@yahoo.com" becomes "abcdefg%40yahoo.com" url encoded.
 
i have tried it, i really don't know whats the matter with it

did you try yourself?
i guess its really on the postData or something. coz in reality there are only 4 arguments w/c wordpress gets, the Author,Email,Website and the Message, but i don't know why still it won't post..

if i remove the Comment_post_ID , no errors, but also no post, if i won't remove it. error 500, @_@
anybody know how to do this one?

help pls
 
Back
Top