使用HttpWebRequest登录Wordpress [英] Login Wordpress using HttpWebRequest
问题描述
我试图在wordpress博客中自动执行一些操作,使用 HttpWebRequest
。
I am trying to automate a few things in wordpress blog, using HttpWebRequest
.
我试过获取登录页面http://mywebsite.net/wp-admin
然后尝试发布页面上有登录数据
and then try to post with login data on page
http://www.mywebsite.net/wp-login.php data =log = admin& pwd = mypassword& wp-submit = Log + In& redirect_to = http%3A %2F%2Fmywebsite.net%2Fwp-admin%2F& testcookie = 1
"http://www.mywebsite.net/wp-login.php"data = "log=admin&pwd=mypassword&wp-submit=Log+In&redirect_to=http%3A%2F%2Fmywebsite.net%2Fwp-admin%2F&testcookie=1"
我无法登录,我有发现当使用浏览器时,cookie被发送到服务器,但是使用HttpWebrequest时,cookie不会在帖子上发送,我为httpwebrequest配置了一个cookiecontainer,并且其他工作正常,...
i am not able to login, wat i have discovered is that when using browser the cookies are sent to the server, but using HttpWebrequest the cookies are not sent on post, i am configured a cookiecontainer for the httpwebrequest and works fine other wise,..
以及发布请求主机也更改为www.mywebsite.net和
get请求它是mywebsite.net
and also on "post" the request host also changes to "www.mywebsite.net" and on "get" request it is "mywebsite.net"
请任何人都可以通过解决方案指导我。
Please can any one guide me through a solution.
推荐答案
你本来应该分享一些代码。但是,我认为有什么一些cookie管理问题。
这是我在向网站提供数据时管理cookie的方式。您可以使用此管理方案代码登录
到您的网站。
You should have been share some code also.However,what I think there are some cookies management issues. This is how I Manage cookies while posing data to the websites .You can use this management scheme code to login to your website.
public string postFormData(Uri formActionUrl, string postData)
{
//Make a HttpWebReguest first
//set cookiecontainer
gRequest.CookieContainer = new CookieContainer();// gCookiesContainer;
//Manage cookies
#region CookieManagement
if (this.gCookies != null && this.gCookies.Count > 0)
{
gRequest.CookieContainer.Add(gCookies);
}
try
{
//logic to postdata to the form
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
//post data logic ends
//Get Response for this request url
try
{
gResponse = (HttpWebResponse)gRequest.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
//check if the status code is ok
if (gResponse.StatusCode == HttpStatusCode.OK)
{
//get all the cookies from the current request and add them to the response object cookies
gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri);
//check if response object has any cookies or not
if (gResponse.Cookies.Count > 0)
{
//check if this is the first request/response, if this is the response of first request gCookies
//will be null
if (this.gCookies == null)
{
gCookies = gResponse.Cookies;
}
else
{
foreach (Cookie oRespCookie in gResponse.Cookies)
{
bool bMatch = false;
foreach (Cookie oReqCookie in this.gCookies)
{
if (oReqCookie.Name == oRespCookie.Name)
{
oReqCookie.Value = oRespCookie.Value;
bMatch = true;
break;
}
}
if (!bMatch)
this.gCookies.Add(oRespCookie);
}
}
}
#endregion
StreamReader reader = new StreamReader(gResponse.GetResponseStream());
string responseString = reader.ReadToEnd();
reader.Close();
return responseString;
}
else
{
return "Error in posting data";
}
}
这篇关于使用HttpWebRequest登录Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!