首页 新闻 会员 周边 捐助

ASP.NET如何根据图片的URL保存到本地

0
悬赏园豆:20 [已解决问题] 解决于 2015-09-23 14:27

需求:在winfrom窗体中,可以根据服务器端图片的URL,将图片保存到本地后显示出来,ASP.NET中却没有权限将服务器上的图片保存到本地,看过别人用Cookie实现过,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _0922
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

string url = string.Format("https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=regist&rand=sjrand&0.5173300584942714");

System.Net.WebRequest imgRequst = System.Net.WebRequest.Create(url);
//以流的方式保存图片

System.Drawing.Image downImage = System.Drawing.Image.FromStream(imgRequst.GetResponse().GetResponseStream());

string dertory = "C:\\test\\";

string fileName = string.Format("{0}.jpg", DateTime.Now.ToString("HHmmssffff"));

if (!System.IO.Directory.Exists(dertory))
{

System.IO.Directory.CreateDirectory(dertory);
}

downImage.Save(dertory + fileName,System.Drawing.Imaging.ImageFormat.Jpeg);

downImage.Dispose();

this.pictureBox1.Image = Image.FromFile(dertory+fileName);

}


}
}

Mask1的主页 Mask1 | 初学一级 | 园豆:113
提问于:2015-09-22 16:02
< >
分享
最佳答案
0

模拟请求 下载图片吧

收获园豆:20
wolfy | 老鸟四级 |园豆:2636 | 2015-09-22 19:56

是的,

Mask1 | 园豆:113 (初学一级) | 2015-09-23 08:33
其他回答(2)
0

这是12306的问题,,你需要使用带证书的http请求。。。

幻天芒 | 园豆:37205 (高人七级) | 2015-09-22 16:35
0

目前遇到的问题貌似解决,供大家参考

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace _0922w
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static CookieContainer Cookies = new CookieContainer();
        static HttpWebRequest myHttpWebRequest;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string oneUrl = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand";

            

            myHttpWebRequest = (HttpWebRequest)WebRequest.Create(oneUrl);//请求的URL
            myHttpWebRequest.CookieContainer = Cookies;//*发送COOKIE
                                                       //if (Session["tzcooket"] != null)
                                                       //    myHttpWebRequest.Headers.Set("Set-Cookie", Session["tzcooket"].ToString());
            myHttpWebRequest.Method = "GET";


           
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

            //获取返回资源
            HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();

            Stream responseStream = response.GetResponseStream();

           
            System.Drawing.Image bitmapImage = System.Drawing.Bitmap.FromStream(responseStream) as System.Drawing.Image;

            bitmapImage.Save("D:\\documents\\visual studio 2015\\Projects\\0922\\0922w\\image\\1152165471.jpg", System.Drawing.Imaging.ImageFormat.Png);

            Image1.ImageUrl = "~/image/1152165471.jpg";



        }
    }

}
Mask1 | 园豆:113 (初学一级) | 2015-09-22 17:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册