View Full Version : webrequest如果获取异步传输数据的页面


轻寒
04-03-2007, 05:16 AM
webrequest和webresponse怎么样才能获取一些有基于ajax产生数据内容的页面?

v-jicwan@prcvap.microsoft.com
04-04-2007, 05:14 AM
ã

ӭMSDNύ⣬ǽᾡ

ʹWebRequestWebResponseȡҳݣο룺
// Remember to add the following using statements to your code
// using System.Net;
// using System.IO;

public static int DownloadFile(String remoteFilename,
String localFilename)
{
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;

// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;

// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();

// Create the local file
localStream = File.Create(localFilename);

// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;

// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read (buffer, 0, buffer.Length);

// Write the data to the local file
localStream.Write (buffer, 0, bytesRead);

// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Close the response and streams objects here
// to make sure they're closed even if an exception
// is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}

// Return total bytes processed to caller.
return bytesProcessed;
}

÷£
int read = DownloadFile("http://www.mysite.com/problem1.jpg",
"d:\\test.jpg");
Console.WriteLine("{0} bytes written", read);

κ⣬ǽ

Jasson Wang
߼ֹ֧ʦ
΢ȫ֧
---------------------------------------------------------------------------------------
ǵķʱ䣺һ9:00-18:00ڼճ⣩ǽգ48СʱṩʼӦһо⡣΢鼼֧Ϣʣhttp://support.microsoft.com/gp/newsgroupsupport/zh-cn.

ʱĶʹáظ(Reply to Group)⽫ûл档
---------------------------------------------------------------------------------------
ԡ״ṩûκεͬʱҲûκȨ

秦风意动
04-04-2007, 11:26 AM
个人觉得楼上代码实现了获取数据
没有 "异步"

封状一个void的方法,处理操作,加上一个
ThreadStart threadStart= new ThreadStart(...);
Thread thread = new Thread(threadStart);
thread.Start();

就算是题目说的异步了.

如果要在异步操作结束得到通知,可以用worker组件.

“v-jicwan@prcvap.microsoft.com”编写:

> 您好:
>
> 欢迎您到MSDN新闻组提交问题,我们将会尽力帮助您。
>
> 使用WebRequest和WebResponse获取页面数据,请参考下述代码:
> // Remember to add the following using statements to your code
> // using System.Net;
> // using System.IO;
>
> public static int DownloadFile(String remoteFilename,
> String localFilename)
> {
> // Function will return the number of bytes processed
> // to the caller. Initialize to 0 here.
> int bytesProcessed = 0;
>
> // Assign values to these objects here so that they can
> // be referenced in the finally block
> Stream remoteStream = null;
> Stream localStream = null;
> WebResponse response = null;
>
> // Use a try/catch/finally block as both the WebRequest and Stream
> // classes throw exceptions upon error
> try
> {
> // Create a request for the specified remote file name
> WebRequest request = WebRequest.Create(remoteFilename);
> if (request != null)
> {
> // Send the request to the server and retrieve the
> // WebResponse object
> response = request.GetResponse();
> if (response != null)
> {
> // Once the WebResponse object has been retrieved,
> // get the stream object associated with the response's data
> remoteStream = response.GetResponseStream();
>
> // Create the local file
> localStream = File.Create(localFilename);
>
> // Allocate a 1k buffer
> byte[] buffer = new byte[1024];
> int bytesRead;
>
> // Simple do/while loop to read from stream until
> // no bytes are returned
> do
> {
> // Read data (up to 1k) from the stream
> bytesRead = remoteStream.Read (buffer, 0, buffer.Length);
>
> // Write the data to the local file
> localStream.Write (buffer, 0, bytesRead);
>
> // Increment total bytes processed
> bytesProcessed += bytesRead;
> } while (bytesRead > 0);
> }
> }
> }
> catch(Exception e)
> {
> Console.WriteLine(e.Message);
> }
> finally
> {
> // Close the response and streams objects here
> // to make sure they're closed even if an exception
> // is thrown at some point
> if (response != null) response.Close();
> if (remoteStream != null) remoteStream.Close();
> if (localStream != null) localStream.Close();
> }
>
> // Return total bytes processed to caller.
> return bytesProcessed;
> }
>
> 调用方法如下:
> int read = DownloadFile("http://www.mysite.com/problem1.jpg",
> "d:\\test.jpg");
> Console.WriteLine("{0} bytes written", read);
>
> 如果您有任何问题,请继续与我们交流。
>
> Jasson Wang
> 在线技术支持工程师
> 微软全球技术支持中心
> ---------------------------------------------------------------------------------------
> 我们的服务时间:周一至周五9:00-18:00(节假日除外)。我们将在两个工作日(48小时)内提供初始回应,并和您一起研究并解决问题。更多微软新闻组技术支持信息,请访问:http://support.microsoft.com/gp/newsgroupsupport/zh-cn.
>
> 回帖时,请在您的新闻组阅读器中使用“回复组(Reply to Group)”,这将帮助其他用户从您的提问中获益。
> ---------------------------------------------------------------------------------------
> 本贴子以”现状”提供且没有任何担保,同时也没有授予任何权利。

v-jicwan@prcvap.microsoft.com
04-06-2007, 02:32 AM
:

?ʲôҪǰ?

Jasson Wang
߼ֹ֧ʦ
΢ȫ֧
---------------------------------------------------------------------------------------
ǵķʱ䣺һ9:00-18:00ڼճ⣩ǽգ48СʱṩʼӦһо⡣΢鼼֧Ϣʣhttp://support.microsoft.com/gp/newsgroupsupport/zh-cn.

ʱĶʹáظ(Reply to Group)⽫ûл档
---------------------------------------------------------------------------------------
ԡ״ṩûκεͬʱҲûκȨ