如何使用HttpWebRequest的,从网站到本地的文件图像拉 [英] How to use httpwebrequest to pull image from website to local file

查看:102
本文介绍了如何使用HttpWebRequest的,从网站到本地的文件图像拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用本地的C#应用​​程序来关闭一个网站一些图像拉来的文件我的本地机器上。我使用下面列出的code。我都试过ASCII编码和UTF8编码,但最终的文件是不是正确的。有谁看到我在做什么错? URL是积极和正确显示图像就好了,当我把地址在我的浏览器。

 私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        HttpWebRequest的lxRequest = (HttpWebRequest)WebRequest.Create(\"http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg\");        //返回值的返回流,然后读入一个字符串
        字符串lsResponse =的String.Empty;
        HttpWebResponse lxResponse =(HttpWebResponse)lxRequest.GetResponse();
        使用(StreamReader的lxResponseStream =新的StreamReader(lxResponse.GetResponseStream()))
        {
            lsResponse = lxResponseStream.ReadToEnd();
            lxResponseStream.Close();
        }        字节[] = lnByte System.Text.UTF8Encoding.UTF8.GetBytes(lsResponse);        System.IO.FileStream lxFS =新的FileStream(34891.jpg,FileMode.Create);
        lxFS.Write(lnByte,0,lnByte.Length);
        lxFS.Close();        MessageBox.Show(完成);
    }


解决方案

良好的形象:D

尝试使用以下code:

您需要使用一个BinaryReader,引起的图像文件是二进制数据,因此不带$ C $光盘UTF或ASCII

编辑:using'ified

  HttpWebRequest的lxRequest =(HttpWebRequest的)WebRequest.Create(
http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg);//返回值的返回流,然后读入一个字符串
字符串lsResponse =的String.Empty;
使用(HttpWebResponse lxResponse =(HttpWebResponse)lxRequest.GetResponse()){
   使用(BinaryReader读卡器=新BinaryReader(lxResponse.GetResponseStream())){
      字节] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
      使用(的FileStream lxFS =新的FileStream(34891.jpg,FileMode.Create)){
          lxFS.Write(lnByte,0,lnByte.Length);
      }
   }
}
MessageBox.Show(完成);

I'm trying to use a local c# app to pull some images off a website to files on my local machine. I'm using the code listed below. I've tried both ASCII encoding and UTF8 encoding but the final file is not an correct. Does anyone see what I'm doing wrong? The url is active and correct and show the image just fine when I put the address in my browser.

    private void button1_Click(object sender, EventArgs e)
    {
        HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create("http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg");

        // returned values are returned as a stream, then read into a string
        String lsResponse = string.Empty;
        HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse();
        using (StreamReader lxResponseStream = new StreamReader(lxResponse.GetResponseStream()))
        {
            lsResponse = lxResponseStream.ReadToEnd();
            lxResponseStream.Close();
        }

        byte[] lnByte = System.Text.UTF8Encoding.UTF8.GetBytes(lsResponse);

        System.IO.FileStream lxFS = new FileStream("34891.jpg", FileMode.Create);
        lxFS.Write(lnByte, 0, lnByte.Length);
        lxFS.Close();

        MessageBox.Show("done");
    }

解决方案

nice image :D

try using the following code:

you needed to use a BinaryReader, 'cause an image file is binary data and thus not encoded in UTF or ASCII

edit: using'ified

HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create(
"http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg");

// returned values are returned as a stream, then read into a string
String lsResponse = string.Empty;
using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse()){
   using (BinaryReader reader = new BinaryReader(lxResponse.GetResponseStream())) {
      Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
      using (FileStream lxFS = new FileStream("34891.jpg", FileMode.Create)) {
          lxFS.Write(lnByte, 0, lnByte.Length);
      }
   }
}
MessageBox.Show("done");

这篇关于如何使用HttpWebRequest的,从网站到本地的文件图像拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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