主题:想使用webrequest下载某网页的内容,但是每次都只能下载到要我登陆的页面,该如何解决?
这是我的源代码:
// Create a request for the URL.
// WebRequest^ request = WebRequest::Create( strURL );
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create( strURL ));
// If required by the server, set the credentials.
request->Credentials = CredentialCache::DefaultCredentials;
request->CookieContainer = gcnew CookieContainer;
// Get the response.
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
response->Cookies = request->CookieContainer->GetCookies( request->RequestUri );
// Display the status.
System::Diagnostics::Trace::WriteLine( response->StatusDescription );
// Get the stream containing content returned by the server.
Stream^ dataStream = response->GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader^ reader = gcnew StreamReader( dataStream, System::Text::Encoding::Default );//这里第二个参数很重要,不然中文的容易返回乱码
// Read the content.
String^ responseFromServer = reader->ReadToEnd();
// Display the content.
System::Diagnostics::Trace::WriteLine( responseFromServer );
System::Diagnostics::Trace::WriteLine( "That's ALL" );
每次都只能返回要求我登陆的页面,即使我已经在浏览器里登录过了也不行。但是如果直接把strURL的内容复制到浏览器里,却可以打开登录后实际的叶面。也就是说,实际上,我的登陆信息已经保存在了本机的某个位置,浏览器把它找了出来,并且用它来顺利的访问这个网站,但是我并不知道这一步是怎么做的。
// Create a request for the URL.
// WebRequest^ request = WebRequest::Create( strURL );
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create( strURL ));
// If required by the server, set the credentials.
request->Credentials = CredentialCache::DefaultCredentials;
request->CookieContainer = gcnew CookieContainer;
// Get the response.
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
response->Cookies = request->CookieContainer->GetCookies( request->RequestUri );
// Display the status.
System::Diagnostics::Trace::WriteLine( response->StatusDescription );
// Get the stream containing content returned by the server.
Stream^ dataStream = response->GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader^ reader = gcnew StreamReader( dataStream, System::Text::Encoding::Default );//这里第二个参数很重要,不然中文的容易返回乱码
// Read the content.
String^ responseFromServer = reader->ReadToEnd();
// Display the content.
System::Diagnostics::Trace::WriteLine( responseFromServer );
System::Diagnostics::Trace::WriteLine( "That's ALL" );
每次都只能返回要求我登陆的页面,即使我已经在浏览器里登录过了也不行。但是如果直接把strURL的内容复制到浏览器里,却可以打开登录后实际的叶面。也就是说,实际上,我的登陆信息已经保存在了本机的某个位置,浏览器把它找了出来,并且用它来顺利的访问这个网站,但是我并不知道这一步是怎么做的。