【问题标题】:compact framework download file from url从 url 下载紧凑框架文件
【发布时间】:2023-03-25 22:05:01
【问题描述】:

我需要更换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

【问题讨论】:

    标签: .net vb.net http windows-mobile


    【解决方案1】:

    请尝试以下代码:

    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 { }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2010-12-22
      相关资源
      最近更新 更多