如何在HttpWebReques下载网站时应用进度条? [英] How to apply progress bar while downloading website by HttpWebReques?

查看:32
本文介绍了如何在HttpWebReques下载网站时应用进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序离线网站下载程序,它只使用提供的URL下载完整的网站。首先,我在给定网站的任何数组中获取所有URL,然后使用HTTPWebRequest下载它们。事情一直很好,直到这里。现在我想在使用所有提取的URL下载网站时应用进度,但我无法找到方法。这是我的代码。首先,我在下载按钮单击时调用此方法。



  public   void  FetchUrl(FetcherArgument arg)
{
if (stop_)
{
返回;
}
lock (urls_)
{
string url = arg.url;
if (SITE_ROOT ==
{
SITE_ROOT = url;
}
if (urls_.Contains(url))
{
返回;
}
// ThreadPool.QueueUserWorkItem(new HttpFetcher()。FetchByWebClient,arg);
ThreadPool.QueueUserWorkItem( new HttpFetcher()。Fetch,arg);
urls_ [url] = true ;
}
}





然后调用方法

public void Fetch( Object thread_content_)
{
if (FetcherController.Get( ).Stop())
{
return ;
}
FetcherArgument arg =(FetcherArgument)thread_content_;
current_ttl_ = arg.ttl;
index_page_ = arg.index_page;
string url = arg.url;
HttpWebRequest request = null ;
HttpWebResponse response = null ;
try
{
request =(HttpWebRequest)WebRequest.Create(url);
}
catch (NotSupportedException)
{
Logger.Log( 网址: + url + 不支持);
return ;
}
尝试
{
response =(HttpWebResponse)request.GetResponse();
}
catch (例外e)
{
Logger.Log( 网址: + url + 无法访问: + e.ToString());
return ;
}
string content_type_primary = arg.content_type_hint;
if (response.ContentType!= null && response.ContentType!= < span class =code-string>
{
content_type_primary = response.ContentType.ToLower();
if (content_type_primary.Contains( ; ))
{
content_type_primary =
content_type_primary.Substring( 0 ,content_type_primary.IndexOf( ;));
}
}
Logger.Log( 获取网址: + url + [content-type + content_type_primary + ]);
switch (content_type_primary)
{
case TEXT_HTML:
ProcessHtmlContent(请求,响应);
break ;
case TEXT_JAVASCRIPT:
case TEXT_ECMASCRIPT:
case APPLICATION_ECMASCRIPT:
case APPLICATION_JAVASCRIPT:
ProcessJavascriptContent(request,response);
break ;
case IMAGE_JPEG:
case IMAGE_GIF:
case IMAGE_PNG:
ProcessImageContent(request,response);
break ;
case TEXT_CSS:
ProcessCssContent(request,response);
break ;
默认
break ;
}
FetcherController.Get()。UrlDone(url);
}



然后处理HTML并在给定方法中获取URL



private void ProcessHtmlContent (HttpWebRequest请求,HttpWebResponse响应)

{

Logger.Log(ProcessHtmlContent:+ request.RequestUri.AbsoluteUri);

byte [ ] html_raw_content = ReadFullResponse(response.GetResponseStream())。ToArray();

编码= GetResponseEncoding(响应);

string html_content = encoding.GetString(html_raw_content);

string new_html_content;

current_url_ = request.RequestUri.AbsoluteUri;

Logger.Log(获取当前网址:+ current_url _); < br $>
found_urls_.Clear();

FetcherArgument [] urls = ExtractHtmlUrls(html_content,out new_html_content);

html_raw_content = encoding.GetBytes(new_html_content) ;

for(int i = 0; i< ; urls.Length; i ++)

{

FetcherController.Get()。FetchUrl(urls [i]);

}

WriteWebNode(request,html_raw_content);

}



并使用此方法保存文件



private void WriteWebNode(HttpWebRequest request,byte [] contents)

{

string path = FetcherController.Get()。GetPrefixDir()+

GetWebNodeFilename(request.RequestUri);

Logger.Log(写入路径:+路径+为url:+ request.RequestUri.AbsoluteUri);

File.WriteAllBytes(路径,内容);



if(index_page_)

{

文件。复制(路径,FetcherController.Get()。GetPrefixDir()+index.html);

}

}



请帮助我如何申请进度条?我知道有WebClient的DownloadString方法,我使用了它,但它没有用。

解决方案

由于HttpWebRequest根本不提供任何进度信息,你可以'吨。你将获得的最好的是Marquee风格的进度条,只显示正在发生的事情。你不能做0到100%吧。



你可以使用进度条的唯一方法就是自己编写请求和下载实现。


正如Dave所提到的,无法从HttpWebRequest方法获取下载状态,但WebClient启用了它。看看这里:

如何:在后台下载文件 [ ^ ]

使用WebClient下载图像文件进度条状态。 [ ^ ]

I am working on an application offline website downloader which downloads the complete website just using the provided URL. Firstly, I am fetching all the URL in any array of the given website and then downloading them using HTTPWebRequest. Things are working fine till here. Now I want to apply progress while downloading website using all the fetched URL but I am unable to find a way to do that. Here is my code. First of all i am calling this method on download button click.

 public void FetchUrl(FetcherArgument arg)
{
    if (stop_)
    {
        return;
    }
    lock (urls_)
    {
        string url = arg.url;
        if (SITE_ROOT == "")
        {
            SITE_ROOT = url;
        }
        if (urls_.Contains(url))
        {
            return;
        }
        //ThreadPool.QueueUserWorkItem(new HttpFetcher().FetchByWebClient, arg);
        ThreadPool.QueueUserWorkItem(new HttpFetcher().Fetch, arg);
        urls_[url] = true;
    }
}



and then call this method

       public void Fetch(Object thread_content_)
   {
        if (FetcherController.Get().Stop())
        {
            return;
        }
        FetcherArgument arg = (FetcherArgument)thread_content_;
        current_ttl_ = arg.ttl;
        index_page_ = arg.index_page;
        string url = arg.url;
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        try
        {
            request = (HttpWebRequest)WebRequest.Create(url);
        }
        catch (NotSupportedException)
        {
            Logger.Log("Url:" + url + " is not supported");
            return;
        }
        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (Exception e)
        {
            Logger.Log("Url:" + url + " is not accessible:" + e.ToString());
            return;
        }
        string content_type_primary = arg.content_type_hint;
        if (response.ContentType != null && response.ContentType != "")
        {
            content_type_primary = response.ContentType.ToLower();
            if (content_type_primary.Contains(";"))
            {
      content_type_primary = 
        content_type_primary.Substring(0,content_type_primary.IndexOf(";"));
            }
        }
        Logger.Log("Fetching url:" + url + "[content-type " + content_type_primary + "]");
        switch (content_type_primary)
        {
            case TEXT_HTML:
                ProcessHtmlContent(request, response);
                break;
            case TEXT_JAVASCRIPT:
            case TEXT_ECMASCRIPT:
            case APPLICATION_ECMASCRIPT:
            case APPLICATION_JAVASCRIPT:
                ProcessJavascriptContent(request, response);
                break;
            case IMAGE_JPEG:
            case IMAGE_GIF:
            case IMAGE_PNG:
                ProcessImageContent(request, response);
                break;
            case TEXT_CSS:
                ProcessCssContent(request, response);
                break;
            default:
                break;
        }
        FetcherController.Get().UrlDone(url);
    }


and then Processing HTML and fetching URLs in the given method

private void ProcessHtmlContent(HttpWebRequest request, HttpWebResponse response)
{
Logger.Log("ProcessHtmlContent: " + request.RequestUri.AbsoluteUri);
byte[] html_raw_content = ReadFullResponse(response.GetResponseStream()).ToArray();
Encoding encoding = GetResponseEncoding(response);
string html_content = encoding.GetString(html_raw_content);
string new_html_content;
current_url_ = request.RequestUri.AbsoluteUri;
Logger.Log("Fetching current url: " + current_url_);
found_urls_.Clear();
FetcherArgument[] urls = ExtractHtmlUrls(html_content, out new_html_content);
html_raw_content = encoding.GetBytes(new_html_content);
for (int i = 0; i < urls.Length; i++)
{
FetcherController.Get().FetchUrl(urls[i]);
}
WriteWebNode(request, html_raw_content);
}

and saving files using this method

private void WriteWebNode(HttpWebRequest request, byte[] contents)
{
string path = FetcherController.Get().GetPrefixDir() +
GetWebNodeFilename(request.RequestUri);
Logger.Log("Writing path:" + path + " for url:" + request.RequestUri.AbsoluteUri);
File.WriteAllBytes(path, contents);

if (index_page_)
{
File.Copy(path, FetcherController.Get().GetPrefixDir() + "index.html");
}
}

Kindly help me how can I apply progress bar ? I know there is DownloadString method of WebClient I used that one but it didnt work out.

解决方案

Since HttpWebRequest doesn't supply any progress information at all, you can't. The best you're going to get is a Marquee style progress bar that just shows something is happening. You can't do the 0 to 100% bar.

The only way you can use a progress bar is if you write the request and download implementation yourself.


As Dave had mentioned, there is not possible to get download status from HttpWebRequest method, but WebClient enables it. Have a look here:
How to: Download a File in the Background[^]
Download Image file using WebClient with progress bar status.[^]


这篇关于如何在HttpWebReques下载网站时应用进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆