开发者

how to maintaine cookies in between two Url's in asp.net

开发者 https://www.devze.com 2023-03-16 00:51 出处:网络
i need pass the cookie values to another Url which have same domain.i have two url\'s with same domai开发者_运维技巧n name..one Url for Authentication another is for retrieve data.i executed first url

i need pass the cookie values to another Url which have same domain.i have two url's with same domai开发者_运维技巧n name..one Url for Authentication another is for retrieve data.i executed first url its authenticated . with this authentication cookie i want to execute second url ..

How to do this...i unable to Add cookie to socond url

here is my code..

string url = "http://172.16.xx.xxx:8080/cms?login&username=santhu&password=welcome"; string url1 = "http://172.16.xx.xxx:8080//cms?status=ProcessStatus"; string result = null;

    try
    {
        WebClient client = new WebClient();
        WebClient client1 = new WebClient();
        result = client.DownloadString(url);
        TextBox1.Text = result.ToString();

        if (Response.Cookies["JSESSIONID"] != null)
            TextBox1.Text = Server.HtmlEncode(Response.Cookies["JSESSIONID"].Value);             

            client1.Headers.Add("JSESSIONID", TextBox1.Text);
            result = client1.DownloadString(url1);
            TextBox2.Text = result.ToString();

    }
    catch (Exception ex)
    {

    }


Set the cookie.Domain = "domain.com" and it works on all url's with that domain.


Here's what you may try. Define a cookie aware web client:

public class WebClientEx : WebClient
{
    public CookieContainer CookieContainer { get; private set; }

    public WebClientEx()
    {
        CookieContainer = new CookieContainer();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = CookieContainer;
        }
        return request;
    }
}

and then use this client for both requests:

using (var client = new WebClientEx())
{
    var values = new NameValueCollection
    {
        { "username", "santhu" },
        { "password", "welcome" },
    };
    // Authenticate
    client.UploadValues("http://172.16.xx.xxx:8080/cms?login", values);

    // Download some secure resource
    var result = client.DownloadString("http://172.16.xx.xxx:8080//cms?status=ProcessStatus");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消