紧凑框架从 url 下载文件

发布于 2024-12-28 22:23:28 字数 921 浏览 2 评论 0原文

我需要更换WebRequestFactory,这是做什么的?有人可以帮我完成这项工作吗?

   Public Shared Sub download_file()
        Dim wr As HttpWebRequest = CType(WebRequestFactory.Create("http://www.test.com/test.jpg"), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        Dim str As Stream = ws.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0
            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
        End While
        Dim fstr As New FileStream("test.jpg", FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
    End Sub 'Main

I need to replace WebRequestFactory, what is this doing? Can someone help me make this work?

   Public Shared Sub download_file()
        Dim wr As HttpWebRequest = CType(WebRequestFactory.Create("http://www.test.com/test.jpg"), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        Dim str As Stream = ws.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0
            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
        End While
        Dim fstr As New FileStream("test.jpg", FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
    End Sub 'Main

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2025-01-04 22:23:28

请尝试以下代码:

public static void getfile(string url, string filename)
{

    try
    {
        string full_url = url + "/" + filename;

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(full_url);
        httpRequest.Credentials = CredentialCache.DefaultCredentials;

        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

        System.IO.Stream dataStream = httpResponse.GetResponseStream();

        // Dim str As Stream = cdsMobileLibrary2.http_download.getfile(filename)

        //10 meg
        byte[] inBuf = new byte[10000001];
        int bytesToRead = Convert.ToInt32(inBuf.Length);
        int bytesRead = 0;
        while (bytesToRead > 0)
        {
           int n = dataStream.Read(inBuf, bytesRead, bytesToRead);
           if (n == 0)
           {
               break; // TODO: might not be correct. Was : Exit While
           }
           bytesRead += n;
           bytesToRead -= n;
        }

        FileStream fstr = new FileStream("\\My Documents\\" + filename, FileMode.OpenOrCreate, FileAccess.Write);
        fstr.Write(inBuf, 0, bytesRead);
        dataStream.Close();
        fstr.Close();


    }
    catch { }
}

Please try the code below:

public static void getfile(string url, string filename)
{

    try
    {
        string full_url = url + "/" + filename;

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(full_url);
        httpRequest.Credentials = CredentialCache.DefaultCredentials;

        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

        System.IO.Stream dataStream = httpResponse.GetResponseStream();

        // Dim str As Stream = cdsMobileLibrary2.http_download.getfile(filename)

        //10 meg
        byte[] inBuf = new byte[10000001];
        int bytesToRead = Convert.ToInt32(inBuf.Length);
        int bytesRead = 0;
        while (bytesToRead > 0)
        {
           int n = dataStream.Read(inBuf, bytesRead, bytesToRead);
           if (n == 0)
           {
               break; // TODO: might not be correct. Was : Exit While
           }
           bytesRead += n;
           bytesToRead -= n;
        }

        FileStream fstr = new FileStream("\\My Documents\\" + filename, FileMode.OpenOrCreate, FileAccess.Write);
        fstr.Write(inBuf, 0, bytesRead);
        dataStream.Close();
        fstr.Close();


    }
    catch { }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文