webclient用于向服务器发出请求但返回ERROR

时间:2014-04-08 13:21:54

标签: c# asp.net asp.net-mvc-3 webclient webclient-download

使用WebClient向服务器发出请求。

WebClient web = new WebClient();
string xmlString = web.DownloadString(partialUrl);

partialUrl返回一个有效的URL,如果我在浏览器中使用它会返回正确的XML列表,但是如果在Visual Studio中从调试模式运行它会抛出错误:

{System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized.
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.Net.WebClient.GetWebRequest(Uri address)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at System.Net.WebClient.DownloadString(String address)
   at Synchroniser.XML.GalwayPortal.Download_POS(String partialUrl, IInterpreter interpreter)

1 个答案:

答案 0 :(得分:2)

来自您的错误您似乎正在使用错误的网址创建网络请求。

请确保在web.DownloadString(partialUrl); url字符串必须以适当的协议开始,如(http,https等)