Httpwebrequest failure in domain authentication download using networkcredential Solution

Source: Internet
Author: User
Tags in domain

Recently, when pwebrequest was used in the project to use networkcredential for domain authentication and download, it never succeeded. Finally, Google's solution found that almost all the solutions discussed were not successful and had to work hard on their own, after the final debugging is passed, the code implementation of the entire solution process is recorded, saving you time to solve similar problems in the future.

 

Key point: you must add contenttype to enable the server to correctly decode it.

Request. contenttype = "application/X-WWW-form-urlencoded ";

 

The specific implementation code is as follows:

 

Void downloadonefilebyhttp (string remoteurl, string localpath, string localurl)

{

Httpwebrequest

Request = httpwebrequest. create (remoteurl) as httpwebrequest; webrequestmethods. HTTP. get; false; new networkcredential (this. username, this. password, this. domain); "application/X-WWW-form-urlencoded"; // very important for authentication

Request. method =

Request. preauthenticate =

Request. Credentials =

Request. contenttype =

 

Memorystream memstream = new memorystream (1024*500 );

 

Byte [] buffer = new byte [1024];

Httpwebresponse response = request. getresponse () as httpwebresponse;

 

Stream reader = response. getresponsestream ();

 

// Create the subfolder

 

If (! System. Io. file. exists (localpath ))

{

System. Io.

Directory. createdirectory (localpath );

}

 

// Write specified bytes array (content) to a file

Filestream newfile = NULL;

 

Try

{

Newfile =

New filestream (localurl, filemode. Create );

 

While (true)

{

Int bytesread = reader. Read (buffer, 0, buffer. Length );

 

If (bytesread = 0)

{

Break;

}

 

Else

{

Memstream. Write (buffer, 0, bytesread );

}

}

If (memstream. length> 0)

{

// Converts the downloaded stream to a byte array

Byte [] downloadeddata = memstream. toarray ();

Newfile. Write (downloadeddata, 0, downloadeddata. Length );

}

}

 

Catch (exception ex)

{

System.

Console. writeline ("exception in HTTP downloading: {0}", Ex. Message );

}

Finally

{

If (newfile! = NULL) newfile. Close ();

 

If (reader! = NULL) reader. Close ();

 

If (response! = NULL) response. Close ();

 

}

}

 

Finally, I would like to remind you: If you want to know why you want to write this code, you can use IE to log on and use httpwatchpro to intercept the content of the entire message, and then use it.. Net code to access and download the same file, and then use a network sniffer such as Winpcap to intercept the content of the entire message. You can easily draw a conclusion by comparing it.

 

Of course, this solution is easy for friends who are familiar with the HTTP protocol and have compiled custom crawlers.

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.