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.