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 with me. Code At least and 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 /// Use com to obtain cookie data.
15 ///   </Summary>
16 ///   <Param name = "url"> Current URL. </Param>
17 ///   <Param name = "cookiename"> Cookiename. </Param>
18 ///   <Param name = "cookiedata"> Used to save cookie data <See CREF = "stringbuilder"/> Instance. </Param>
19 ///   <Param name = "size"> Cookie size. </Param>
20 ///   <Returns> If yes, return <C> True </C> Otherwise, return <C> False </C> . </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 /// Get current <See CREF = "Uri"/> Of <See CREF = "cookiecontainer"/> Instance.
27 ///   </Summary>
28 ///   <Param name = "Uri"> Current <See CREF = "Uri"/> Address. </Param>
29 ///   <Returns> Current <See CREF = "Uri"/> Of <See CREF = "cookiecontainer"/> Instance. </Returns>
30 Public   Static Cookiecontainer geturicookiecontainer (URI) {
31 Cookiecontainer cookies =   Null ;
32
33 // Defines the size of cookie data.
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 accommodate 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 }

Is it quite simple? I hope it will be helpful to everyone.

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.