问题
I'm doing a C# windows application, and need to know how to download a picture which has a link with a query string, for example www.mywebsite.com/img.aspx?imgid=12345
(which automatically redirects to the appropriate image based on imgid
). I need to then save the file to the disk somewhere. I don't need any big fancy download manager, just the cleanest way to get such an image from a redirecting URL.
回答1:
You should be able to do it like this:
using (WebClient Client = new WebClient ())
{
Client.DownloadFile("http://www.mywebsite.com/img.aspx?imgid=12345", "12345.jpg");
}
回答2:
Use the WebClient class and he DownloadFile method. http://msdn.microsoft.com/en-us/library/ez801hhe.aspx
It's perfectly acceptable to pass a querystring parameter in the URI of the method.
回答3:
Your file will be at bin/debug
folder, and give the extension in which your browser download the file. like
{
using (WebClient client = new WebClient()) {
client.DownloadFile("http://www.mywebsite.com/img.aspx?imgid=12345", "selectedfile.gif");
}
}
来源:https://stackoverflow.com/questions/8197294/how-to-download-an-image-using-a-url-with-a-query-string-instead-of-image-path