CookieContainer obtained through COM, which is simple and easy to use.

Source: Internet
Author: User

Recently I am working on a small software program that implements login and automatic sending of information through HttpWebRequest. Although it has not yet been implemented, today I have seen a good way to get CookieContainer from a foreign website, share it out. I have seen the least code and it is very good.

 

1 using System;
2 using System. Runtime. InteropServices;
3 using System. Text;
4 using System. Net;
5
6 namespace NExplus. NSiter
7 {
8/** // <summary>
9 // method class for obtaining cookies.
10 /// </summary>
11 public class CookieManger
12 {
13/** // <summary>
14 // obtain Cookie data through COM.
15 /// </summary>
16 /// <param name = "url"> current url. </Param>
17 /// <param name = "cookieName"> CookieName. </param>
18 // <param name = "cookieData"> the <see cref = "StringBuilder"/> instance used to save Cookie Data. </Param>
19 /// <param name = "size"> Cookie size. </Param>
20 /// <returns> If the call succeeds, <c> true </c> is returned. Otherwise, <c> false </c> is returned. </Returns>
21 [DllImport ("wininet. dll", SetLastError = true)]
22 public static extern bool InternetGetCookie (
23 string url, string cookieName,
24 StringBuilder cookieData, ref int size );
25/*** // <summary>
26 // obtain the current <see cref = "Uri"/> <see cref = "CookieContainer"/> instance.
27 /// </summary>
28 // <param name = "uri"> current <see cref = "Uri"/> address. </Param>
29 // <returns> current <see cref = "Uri"/> <see cref = "CookieContainer"/> instance. </Returns>
30 public static CookieContainer GetUriCookieContainer (Uri uri ){
31 CookieContainer cookies = null;
32
33 // define the Cookie data size.
34 int datasize = 256;
35 StringBuilder cookieData = new StringBuilder (datasize );
36
37 if (! InternetGetCookie (uri. ToString (), null, cookieData,
38 ref datasize )){
39 if (datasize <0)
40 return null;
41
42 // make sure there is enough space to hold Cookie data.
43 cookieData = new StringBuilder (datasize );
44 if (! InternetGetCookie (uri. ToString (), null, cookieData,
45 ref datasize ))
46 return null;
47}
48
49
50 if (cookieData. Length> 0 ){
51 cookies = new CookieContainer ();
52 cookies. SetCookies (uri, cookieData. ToString (). Replace (';',','));
53}
54 return cookies;
55}
56
57}
58}

 

 

Or another method

 

// Add this to store Cookies directly.
HttpWebRequest myHttpWebRequest = WebRequest. Create (url) as HttpWebRequest;
MyHttpWebRequest. ContentType = "application/x-www-form-urlencoded ";
MyHttpWebRequest. Method = "GET ";
MyHttpWebRequest. Timeout = 60*1000;
If (myCookieContainer = null)
{
MyCookieContainer = new CookieContainer ();
}
MyHttpWebRequest. CookieContainer = myCookieContainer;

/// <Summary>
/// Set to retrieve page Session/Cookies
/// </Summary>
Private CookieContainer MyCookieContainer
{
Set {myCookieContainer = value ;}
Get {return myCookieContainer ;}
}

// In fact, you must use Com

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.