如何使用Cookie在浏览器中打开页面? [英] How to open page in browser with cookie?

查看:107
本文介绍了如何使用Cookie在浏览器中打开页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个网页(ASP.NET MVC),在其中获取一些资源的cookie。

For example, I have a web page (ASP.NET MVC), where I get cookie for some resource.

我的MVC控制器操作代码:

My MVC controller action code:

        CookieContainer cookies = new CookieContainer();
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;
        string data = "<Request><MsgType>Authenticate</MsgType><SubMsgType>Login</SubMsgType><UserID>MYLOGIN</UserID><passwordNotEncrypted>MYPASSWORD</passwordNotEncrypted></Request>";
        StringContent content = new StringContent(data, Encoding.UTF8, "text/xml");

        HttpClient client = new HttpClient(handler);
        Uri uri = new Uri("https://address/browserservices.aspx/login");
        HttpResponseMessage response = client.PostAsync(uri, content).Result;

此请求将auth cookie设置为 cookies 变量。然后下一个请求可以正常工作:

this request set auth cookie to cookies variable. And next request works fine:

        var result = client.GetAsync("https://address/RemoteSupport.aspx?id=bla-bla-bla&pltFrmType=Android&agentversion=13.46").Result;
        var text = result.Content.ReadAsStringAsync().Result;

(如果我在不使用cookie的情况下将其命名为未经授权响应)

(if I call it without cookie I get Unauthorized response)

现在我想重定向到此 https://address/RemoteSupport.aspx?id = bla-bla -bla& pltFrmType = Android& agentversion = 13.46 带cookie的地址。因此,用户应查看重定向到该地址的地址。

Right now I want to do a redirect to this https://address/RemoteSupport.aspx?id=bla-bla-bla&pltFrmType=Android&agentversion=13.46 address with cookie. So, user should look at it redirected to this address. How to do it?

我尝试过:

        foreach (Cookie cookie in responseCookies)
        {
            Response.Cookies.Append(cookie.Name, cookie.Value);
        }

        return Redirect($"https://address/RemoteSupport.aspx?id={id}&pltFrmType=Android&agentversion=13.46");

但不起作用

推荐答案

您不能只是执行重定向。在MVC中,它向浏览器返回302状态代码,指示其向第三方网站发出自己的请求。

You can't just do a Redirect. In MVC that returns a 302 status code to the browser that instructs it to make it's own request to the 3rd party site.

使用<$ c $的相同实例c> HttpClient ,您需要向第三方服务器发出第二个请求。这应该会自动将cookie添加到新请求中,然后您可以设置答案的格式并将其传递给浏览器客户端。

Using the same instance of the HttpClient you need to make a second request to the 3rd party server. This should automatically add the cookie to the new request, and you can then format the answer and pass it on to your browser client.

理想情况下,您不应实例化HttpClient。该方法,因为这意味着您每次必须发出2个请求。也许是这样的:

Ideally you should not instantiate the HttpClient in the method, as this means that you have to make 2 requests each time. Perhaps something like this:

public class MyController : Controller
{
    private static readonly HttpClient _httpClient;

    public MyController()
    {
        if (_httpClient == null)
        {
            CookieContainer cookies = new CookieContainer();
            HttpClientHandler handler = new HttpClientHandler();
            handler.CookieContainer = cookies;
            _httpClient = new HttpClient(handler);

            LoginWithClient();
        }
    }

    private void LoginWithClient()
    {
        string data = "<Request><MsgType>Authenticate</MsgType><SubMsgType>Login</SubMsgType><UserID>MYLOGIN</UserID><passwordNotEncrypted>MYPASSWORD</passwordNotEncrypted></Request>";
        StringContent content = new StringContent(data, Encoding.UTF8, "text/xml");  
        Uri uri = new Uri("https://address/browserservices.aspx/login");
        HttpResponseMessage response = client.PostAsync(uri, content).Result; 
        // Maybe check the result here, but we should have the cookie by now
    }

    public JsonResult MyAction()
    {
        var result = client.GetAsync("https://address/RemoteSupport.aspx?id=bla-bla-bla&pltFrmType=Android&agentversion=13.46").Result;
        var text = result.Content.ReadAsStringAsync().Result;
        return Json(text, JsonRequestBehavior.AllowGet);
    }

这应将原始文本字符串返回到浏览器,您可以在其中显示它,或将其解析为Json或XML或其他内容,然后使用数据。

This should return the raw text string to the browser where you can display it, or parse it as Json or XML or whatever and work with the data.

这篇关于如何使用Cookie在浏览器中打开页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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