Simulate ie login to a website that requires (Windows identity) Authentication

Source: Internet
Author: User

User Authentication

Httpwebrequest can be implemented.

The simplest method is to put the user name and password in the URL.

For example: http: // username: password@www.abc.com/

A complicated method is to add authorization in the request header. Format: Basic + base64 (username: password)

 

For example:

 

Public static string senddatabyget (string URL, string postdatastr, ref cookiecontainer cookie)
{
Try
{
String url = URL + (postdatastr = ""? "":"? ") + Postdatastr;
Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
// If (cookie. Count = 0)
//{
// Request. cookiecontainer = new cookiecontainer ();
// Cookie = request. cookiecontainer;
//}
// Else
//{
Request. cookiecontainer = cookie;
//}
Request. keepalive = true;
Request. headers. Add ("Authorization", "Basic ym9uc29uomjvbnnvbg = ");
Request. cookiecontainer = cookie;
Request. Credentials = credentialcache. defaultcredentials;

Request. method = "get ";
// Request. Timeout = 1000*300;
Request. contenttype = "text/html; charset = gb2312 ";
Request. useragent = "Mozilla/4.0 (compatible; MSIE 7.0;   Windows   NT 5.1; Trident/4.0; myie2 0.3; Mogi/1;   . Net   CLR 2.0.50727;. net   CLR 3.0.04506.648;. Net CLR 3.5.21022;. Net CLR 3.0.20.6.2152;. Net CLR 3.5.30729 )";

Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream myresponsestream = response. getresponsestream ();
Streamreader   Mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("gb2312 "));
String retstring = mystreamreader. readtoend ();
Mystreamreader. Close ();
Myresponsestream. Close ();

Return retstring;
}
Catch (exception ex)
{
Return "submission error:" + ex. message;
}
}

Related Article

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.