문제

The site provides links as http://www.example.com/download.php?id=53979 . I know that this is a pdf file and want to download it via a C# program. Is this possible and if yes, how?

도움이 되었습니까?

해결책

In order to download a file, you simply need to use the WebClient object like in the question referenced above:

using (var client = new WebClient())
    client.DownloadFile("http://www.datasheet4u.com/download.php?id=53979", "datasheet.pdf");

What makes your case slightly different has nothing to do with the server being written in PHP or anything like that. The link you provided (http://www.datasheet4u.com/datasheet/L/M/7/LM741_NationalSemiconductor.pdf.html) appears to be checking Referer headers when serving the file. This is likely some attempt on their part to prevent what you're trying to do, but it doesn't actually prevent it.

All you need to do is add a Referer header to the request. Something like this:

using (var client = new WebClient())
{
    client.Headers.Add("Referer","http://www.datasheet4u.com/datasheet/L/M/7/LM741_NationalSemiconductor.pdf.html");
    client.DownloadFile("http://www.datasheet4u.com/download.php?id=53979", "datasheet.pdf");
}

The method for downloading the file is still the same. The server just requires that you send an extra piece of information in the request.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top