我在访问以下网址时遇到了一个奇怪的问题:
这是代码:
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'
据我所知网址
格式良好。
我试过的所有浏览器都可以处理它。
使用 Fiddler 进行的测试表明,没有从 Windows Phone 模拟器发送 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 条目。
有任何想法吗 ?