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.