重定向HTTP发布 [英] Redirect HTTP Post

查看:73
本文介绍了重定向HTTP发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我,在找到重定向URI之后,下一步该怎么做?

从OP更新:
感谢您的答复,我引用的网址如何在.NET中使用HttpWebRequest和HttpWebResponse [ ^ ]

但我被困在一个地方.

我的意图如下,说我要登录到Wordpress.

1.我向 http://wordpress [ ^ ]

2.然后我的POST网址是 https://en.wordpress.com/wp-login.ph [<带有登录参数的href ="https://en.wordpress.com/wp-login.ph" target ="_ blank" title =新窗口"> ^ ]

3.但是这里有一个重定向,所以,我想知道,在第一步之后,我需要做什么?如果我由于重定向而发布带有未登录参数的webrequest,我不知道该如何进行....

Can anybody tells me that after finding a redirection URI, how to do the next step?

UPDATE from OP:
Thanks for the reply, I am refering the url How to use HttpWebRequest and HttpWebResponse in .NET[^]

but I am stuck in a place.

My intension is as below, say I a going to login to Wordpress.

1. I gave a web request to http://wordpress[^]

2. Then my POST url is https://en.wordpress.com/wp-login.ph[^] with login parameters

3. But there is a redirection here, so, I wanted to know, after the step one what I have to do? If I post the webrequest with parameters its not login in because of the redirection, I dont know how to proceed....

推荐答案

这可能就是你所需要的.重新寻找:

This might be what you''re looking for:

var wr = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://wordpress.com/"));
wr.AllowAutoRedirect = true;



VS元数据引发此问题:



VS metadata throws up this:

// Summary:
//     Gets or sets a value that indicates whether the request should follow redirection
//     responses.
//
// Returns:
//     true if the request should automatically follow redirection responses from
//     the Internet resource; otherwise, false. The default value is true.
public bool AllowAutoRedirect { get; set; }


我回答了一个与之前类似的问题.其中一些可能会有所帮助.您可以在通过C#下载没有直接地址的文件中找到它.程序 [ ^ ].

这可能非常困难,而我的工作方式是在Firefox中使用HttpFox来监视Firefox在执行此操作时遵循的实际HTTP请求.您需要注意的是引荐来源网址和Cookies.

Wordpress与另一篇文章中的有所不同,因此这是我做的并使用的它,我能够获得https://en.wordpress.com网站的HTML(使用实际值填写日志和pwd登录凭据).

I answered a question similar to this earlier. Some of it may be helpful. You can find it at Downloading files without direct address through C# program[^].

This can be very difficult and the way I worked it was to use HttpFox within Firefox to monitor the actual HTTP requests that Firefox followed when doing this. The things that you need to watch for are the Referrer and the Cookies.

Wordpress is a bit different than in the other article, so here''s what I did and using it, I was able to get the HTML of the https://en.wordpress.com site (filling in log and pwd with actual login credentials).

//Set up the request
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(@"https://en.wordpress.com/wp-login.php");
myRequest.Referer = @"https://en.wordpress.com";
myRequest.Method = "POST";
myRequest.Host = "en.wordpress.com";

//set up the post data
string postData=@"log=username&pwd=password&testcookie=1&redirect_to=https://wordpress.com&rememberme=forever&submit";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
myRequest.ContentLength = data.Length;

//create the cookie container
myRequest.CookieContainer = new CookieContainer();

//finish setting up the request
myRequest.ContentType ="application/x-www-form-urlencoded";
myRequest.AllowAutoRedirect = true;

//send the data
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

//get the response
string pageSource = "";

using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        pageSource = reader.ReadToEnd();
    }
}


这篇关于重定向HTTP发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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