HttpWebRequest的代码和用法 [英] HttpWebRequest Codes and Usage

查看:125
本文介绍了HttpWebRequest的代码和用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个小问题,希望您能帮助我.

基本上.我知道如何使用httpwebrequest在不同的站点中登录.

但是现在,我正在处理类似的东西.我想评论我的一个wordpress网站,

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)



但是我被帖子响应困住了,我得到了一个错误.
远程服务器返回错误:(500)内部服务器错误.

但是当我尝试使用登录帖子数据的格式登录wordpress时,它会记录我,

这是您可以在LiveHttpHeaders上看到的



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,

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



试图删除& comment_post_ID,它不会给我一个错误,但是也没有给我一个结果,就像空白结果一样.


ReferURL最初是一个url,我只是暂时将其替换为"url",但实际上是内部的url



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?

推荐答案

如果您愿意使用开放源代码,则可以在c#中使用WordpressWrapper.它在下面使用wordpress api.

另外,您可以将此作为参考并自己编写一些代码.

HttpWebRequest在许多情况下是最好的,但是使用wordpress api会比HttpWebRequest容易.

http://joeblogs.codeplex.com/ [ ^ ]
http://www.alexjamesbrown.com/development/using-joeblogs/ [
If you are open to use opensource for your purpose you can use following WordpressWrapper in c#. It uses wordpress api underneath.

Alternatively, you can keep this as reference and write some code yourself.

HttpWebRequest is best in many cases, however it would be easier to use wordpress api''s than HttpWebRequest.

http://joeblogs.codeplex.com/[^]
http://www.alexjamesbrown.com/development/using-joeblogs/[^]



这篇关于HttpWebRequest的代码和用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆