简体   繁体   English

C#WPF WebClient.DownloadString()没有返回任何内容

[英]C# WPF WebClient.DownloadString() not returning anything

I started off with the simple code below in order to grab the html from webpages into a string to later process. 我从下面的简单代码开始,以便将网页中的html抓取到字符串中以便稍后处理。 For some sites like Digikey it works but for others like Mouser it doesn't. 对于像Digikey这样的网站来说,它可以运行,但对于像Mouser这样的其他网站却没有。

I have tried putting headers and userAgents onto the WebClient along with converting the url to a Uri with no success. 我已经尝试将header和userAgent放到WebClient上,同时将url转换为Uri但没有成功。 Does anybody have any other suggestions of what I could try? 有没有人对我能尝试什么有任何其他建议? Or could anybody try to get the code to work and let me know how it goes? 或者任何人都可以尝试让代码工作,让我知道它是怎么回事?

String url = "http://www.mouser.com/ProductDetail/Vishay-Thin-Film/PCNM2512E1000BST5/? 
qs=sGAEpiMZZMu61qfTUdNhG6MW4lgzyHBgo9k7HJ54G4u10PG6pMa7%252bA%3d%3d"    
WebClient web = new WebClient();
String html = web.DownloadString(url);
MessageBox.Show(html);

EDIT : The link should lead here: link 编辑:链接应该在这里: 链接

EDIT : I tried the following chunk of code with no luck: 编辑:我尝试了以下大块代码,没有运气:

String url = "http://www.mouser.com/ProductDetail/Vishay-Thin-Film/PCNM2512E1000BST5/? 
qs=sGAEpiMZZMu61qfTUdNhG6MW4lgzyHBgo9k7HJ54G4u10PG6pMa7%252bA%3d%3d"    
WebClient web = new WebClient();
web.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
String html = web.DownloadString(url);
MessageBox.Show(html);

Need to download Fiddler it's free (was originally developed by Microsoft) and it lets you record browser sessions. 需要免费下载Fiddler (最初由微软开发),它可以让你记录浏览器会话。 So launch it open chrome or whatever your browser is and go though the steps. 因此,启动它打开chrome或任何浏览器,然后执行步骤。 Once you done you can stop it and look at every request and response and the raw data sent. 完成后,您可以停止它并查看每个请求和响应以及发送的原始数据。

Makes it easy to spot the difference between your code and the browser. 可以轻松发现代码和浏览器之间的差异。

There are also many free tools that will take your request/response data and generate the C# code for you such as Request To Code . 还有许多免费工具可以获取您的请求/响应数据并为您生成C#代码,例如Request To Code That is not the only one, I'm not at work and I can't recall the one I use there, but there are plenty to choose from. 这不是唯一一个,我不在工作,我不记得我在那里使用的那个,但有很多可供选择。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM