我在访问以下网址时遇到了一个奇怪的问题:
这是代码:
void WebRequestButton_Click( object sender, RoutedEventArgs e )
{
string url = "http://xn--fanbys-exa.org/episodes.m4a.rss";
if ( Uri.IsWellFormedUriString( url, UriKind.Absolute ) )
{
HttpWebRequest webRequest = ( HttpWebRequest )HttpWebRequest.Create( url );
if ( webRequest != null )
{
webRequest.BeginGetResponse( new AsyncCallback( ReadWebRequestCallback ), webRequest );
}
}
}
private void ReadWebRequestCallback( IAsyncResult callbackResult )
{
HttpWebRequest request = ( HttpWebRequest )callbackResult.AsyncState;
HttpWebResponse response = null;
try
{
response = ( HttpWebResponse )request.EndGetResponse( callbackResult );
}
catch ( Exception e )
{
System.Diagnostics.Debug.WriteLine( e );
}
if ( response != null )
{
using ( StreamReader httpwebStreamReader = new StreamReader( response.GetResponseStream( ) ) )
{
string results = httpwebStreamReader.ReadToEnd( );
System.Diagnostics.Debug.WriteLine( results );
}
response.Close( );
}
}
使用request.EndGetResponse()读取响应时会引发此异常:
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll
System.ArgumentException ---> System.ArgumentException: The parameter is incorrect.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.WebRequest_Send(InternalWebRequest request)
at MS.Internal.InternalWebRequest.Send()
at System.Net.Browser.ClientHttpWebRequest.PrepareAndSendRequest(String method, Uri requestUri, Stream requestBodyStream, WebHeaderCollection headerCollection, CookieContainer cookieContainer)
at System.Net.Browser.ClientHttpWebRequest.BeginGetResponseImplementation()
at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(Async'UI Task' (Managed): Loaded 'System.Runtime.Serialization.dll'
据我所知,该URL
是格式正确的。
所有我试过的浏览器都可以处理它。
使用Fiddler测试显示,从Windows手机模拟器中没有发送任何HTTP请求。
如果已尝试多个不同的URL(使用活动的Fiddler实例):
string url = "http://xn--fanbys-exa.org/episodes.m4a.rss";
// not ok --> System.ArgumentException: The parameter is incorrect.
string url = "http://xn--fanbys-exa.org";
// not ok --> System.ArgumentException: The parameter is incorrect.
string url = Uri.EscapeUriString( "http://xn--fanbys-exa.org/episodes.m4a.rss" );
// not ok --> System.ArgumentException: The parameter is incorrect.
string url = "http://stackoverflow.com";
// ok, HTTP request is sent and response is received
string url = "http://stack--overflow.com";
// ok, HTTP request is sent and response is received
string url = "http://xn--stackoverflow.com";
// ok, HTTP request is sent and response 502 is received
// --> System.Net.WebException: The remote server returned an error: NotFound.
问题可以在模拟器和设备上重现。
我的猜测是这可能与DNS有关。
我无法控制参考网站及其DNS条目。
有什么想法吗?