Get WebBrowser full cookie and HttpWebRequest get page data asynchronously

Source: Internet
Author: User

Get WebBrowser Full Cookie

[DllImport ("Wininet.dll", CharSet = CharSet.Auto, SetLastError = True)]
static extern bool Internetgetcookieex (string Pchurl, String pchcookiename, StringBuilder pchcookiedata, ref int Pcchcook Iedata, int dwFlags, object lpreserved);
The cookie is removed and can be retrieved after login
private static string getcookiestring (string url)
{
Determine the size of the cookie
int datasize = 256;
StringBuilder cookiedata = new StringBuilder (datasize);
if (! Internetgetcookieex (URL, null, Cookiedata, ref datasize, 0x00002000, NULL))
{
if (DataSize < 0)
return null;
Allocate StringBuilder large enough to hold the cookie
Cookiedata = new StringBuilder (datasize);
if (! Internetgetcookieex (URL, null, Cookiedata, ref datasize, 0x00002000, NULL))
return null;
}
return cookiedata.tostring ();
}

Call the Getcookiestring method directly to get the cookie

HttpWebRequest fetching page Data asynchronously

private static ManualResetEvent Alldone = new ManualResetEvent (false);

Private Const int buffer_size = 1024;
public static void Asyncresponse (string url, cookiecollection cookiecollection)
{
Get the URI from the command line
Uri httpsite = new uri (URL);

Create Request Object
HttpWebRequest wreq = (HttpWebRequest) webrequest.create (httpsite);
Wreq. Method = "GET";

if (cookiecollection! = null)
{
Wreq. Cookiecontainer = new Cookiecontainer ();
Wreq. Cookiecontainer.add (cookiecollection);
}
Create a State object
RequestState rs = new requeststate ();

Add the request to the State so that it can be passed back and forth
Rs. Request = wreq;

Making an asynchronous request
IAsyncResult r = (IAsyncResult) wreq. BeginGetResponse (New AsyncCallback (Respcallback), RS);

Set ManualResetEvent to Wait,
So that the application does not exit before calling the callback
Alldone.waitone ();


}

private static void Respcallback (IAsyncResult ar)
{
Getting a RequestState object from an asynchronous result
RequestState rs = (requeststate) ar. asyncstate;

Get HttpWebRequest from RequestState
HttpWebRequest req = Rs. Request;

Call EndGetResponse to generate the HttpWebResponse object
The object is from the request made above
HttpWebResponse resp = (httpwebresponse) req. EndGetResponse (AR);

Now that we have a response, it's time to
The response stream begins to read the data.
Stream Responsestream = resp. GetResponseStream ();

This read operation also uses asynchronous completion, so we
is going to store the stream in RequestState
Rs. Responsestream = Responsestream;

Please note that Rs. Bufferread is passed to BeginRead.
This is where the data will be read into.
IAsyncResult Iarread = Responsestream.beginread (rs. Bufferread, 0, Buffer_size, new AsyncCallback (Readcallback), RS);
}


private static void Readcallback (IAsyncResult asyncResult)
{
Get RequestState object from AsyncResult
RequestState rs = (requeststate) asyncresult.asyncstate;

Remove the Responsestream set in the Respcallback
Stream Responsestream = Rs. Responsestream;

At this time Rs. There should be some data in the Bufferread.
The read operation will tell us if there is any data
int read = Responsestream.endread (AsyncResult);

if (Read > 0)
{
Prepares a Char array buffer for conversion to Unicode
char[] Charbuffer = new Char[buffer_size];

Converts a byte stream to a Char array and then converts to a string
Len shows how many characters are converted to Unicode

int len = Rs. Streamdecode.getchars (Rs. Bufferread, 0, read, charbuffer, 0);
String str = new string (charbuffer, 0, Len);

Appends the recently read data to the RequestData StringBuilder object,
The object is contained in the RequestState
Rs. Requestdata.append (str);


Now make another asynchronous call to read more data
Note that this process will continue to be called until
Responsestream.endread Return-1
IAsyncResult ar = Responsestream.beginread (rs. Bufferread, 0, Buffer_size, new AsyncCallback (Readcallback), RS);
}
Else
{
if (Rs. Requestdata.length > 1)
{
All data has been read, so it is displayed to the console
String strcontent;
Strcontent = Rs. Requestdata.tostring ();
Console.WriteLine (strcontent);
}

Turn off response flow
Responsestream.close ();

Set ManualResetEvent so that the main thread can exit
Alldone.set ();
}
Return
}

public class RequestState
{
const int buffer_size = 1024;
Public StringBuilder RequestData;
Public byte[] Bufferread;
Public HttpWebRequest Request;
Public Stream Responsestream;
Create a decoder of the appropriate encoding type
Public Decoder Streamdecode = Encoding.UTF8.GetDecoder ();

Public RequestState ()
{
Bufferread = new Byte[buffer_size];
RequestData = new StringBuilder ("");
Request = null;
Responsestream = null;
}
}

Get WebBrowser full cookie and HttpWebRequest get page data asynchronously

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.