HttpWebRequest returning “The remote server return

2020-06-04 12:35发布

I am trying to authenticate against the Huddle API using the Windows Phone 7 Emulator. However, I am not getting any success. I keep getting "The remote server returned an error: NotFound". I have even tried "dumbing down" my code and just trying a straight web site, eg. Google but still get the same result.

I have the following code:

string url = "http://www.google.com"; 

 HttpWebRequest client= WebRequest.CreateHttp(new Uri(url)) as HttpWebRequest;

 client.AllowReadStreamBuffering = true;

 // Call and handle the response.
 client.BeginGetResponse(
  (asResult) =>
  {
   Dispatcher.BeginInvoke(
    () =>
    {
     try
     {
      var response = client.EndGetResponse(asResult);
      System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
      string responseString = reader.ReadToEnd();

     }
     catch (WebException failure)
     {
      throw failure;
     }
    });
  },
   null
 );

Execution always ends up in the catch section. However, having watched Fiddler2, there seems not to be any traffic at all to google.com. So the request doesn't seem to be being made.

I've seen a similar problem here Retrieve XML from https using WebClient/HttpWebRequest - WP7, but I am using a standard port so not sure this is relevant. I have also tried simplifying the code as per the post, but no success.

Interestingly, the most likely option seems to be because I may not have Network Capabilities defined in my AppManifestWM.xaml file as per HttpWebRequest Breaks On WP7, but my AppManifestWM.xaml file appears to have this defined:

<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0">
  <App xmlns="" ProductID="{ac5b5d62-573c-4134-b290-0ad4f678ad7f}" Title="xxx.WindowsPhone7.Client" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal"  Author="xxx.WindowsPhone7.Client author" Description="Sample description" Publisher="xxx.WindowsPhone7.Client publisher">
    <IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath>
    <Capabilities>
      <Capability Name="ID_CAP_NETWORKING" />
      <Capability Name="ID_CAP_LOCATION" />
      <Capability Name="ID_CAP_SENSORS" />
      <Capability Name="ID_CAP_MICROPHONE" />
      <Capability Name="ID_CAP_MEDIALIB" />
      <Capability Name="ID_CAP_GAMERSERVICES" />
      <Capability Name="ID_CAP_PHONEDIALER" />
      <Capability Name="ID_CAP_PUSH_NOTIFICATION" />
      <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
    </Capabilities>
    <Tasks>
      <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
    </Tasks>
    <Tokens>
      <PrimaryToken TokenID="xxx.WindowsPhone7.ClientToken" TaskName="_default">
        <TemplateType5>
          <BackgroundImageURI IsRelative="true" IsResource="false">Background.png</BackgroundImageURI>
          <Count>0</Count>
          <Title>xxx.WindowsPhone7.Client</Title>
        </TemplateType5>
      </PrimaryToken>
    </Tokens>
  </App>
</Deployment>

So I'm at a loss. The request doesn't actually seem to be occurring, leading me to think something is preventing it.

Update:

Nothing changed, but thought this stack trace might heko:

System.Net.WebException was unhandled Message=The remote server returned an error: NotFound. StackTrace: at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at xxx.WindowsPhone7.Client.Views.AddHuddleUserPage.<>c__DisplayClass2.<>c__DisplayClass4.b__1() at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Delegate.DynamicInvokeOne(Object[] args) at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) at System.Delegate.DynamicInvoke(Object[] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) at System.Windows.Threading.Dispatcher.OnInvoke(Object context) at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

The status is System.Net.WebExceptionStatus.UnknownError

Thanks for your time.

6条回答
在下西门庆
2楼-- · 2020-06-04 12:47

I worked several hours with the exact same symptoms as the original poster. Then I closed Fiddler2 as suggested above. And then it works. No more "The remote server returned and error: NotFound."

Magic! Thank you, kellyb. Should have upvoted but I do not have enough credit.

查看更多
乱世女痞
3楼-- · 2020-06-04 12:52

Never used Fiddler2 but had exact the same problem when developing a Windows Phone app.

For me the cause was quite different: The WMAppManifest.xml was just missing ID_CAP_NETWORKING!

Since I got the "not found" exception and not a "not supported exception" I tried almost everything else until I found the real cause of the problem ... ;-)

I prefer disabling all CAPS and only enable the ones the app really needs since users will not understand/accept if an app needs access to "everything" ;-)

查看更多
男人必须洒脱
4楼-- · 2020-06-04 13:00

This may just be a matter of resolving your network connectivity.

Can you access the web via IE and a WebBrowser control inside an app?

You may have a proxy in the way. See if this doco helps in that regard.

Proxy Support for Windows Phone Emulator

查看更多
贪生不怕死
5楼-- · 2020-06-04 13:09

Shutting down Fiddler2 solved my issue.

查看更多
我想做一个坏孩纸
6楼-- · 2020-06-04 13:10

Do you have Fiddler running? I get this error repeatedly when Fiddler is attached to my networking stack. With Fiddler off, no issues.

I would be interested in hearing the why's behind that if someone knows...

Cheers

查看更多
【Aperson】
7楼-- · 2020-06-04 13:10

Ok, I've solved it ... but don't know how. My machine has not been rebooted, my code has not changed. The only possible explanation is my emulator did crash a few times. Maybe something in there.

Thanks for your time, this is the code I'm using, which works well with the Huddle API:

                string url = "https://api.huddle.net/v1/xml/workspaces"; ; 

                HttpWebRequest client= WebRequest.CreateHttp(new Uri(url)) as HttpWebRequest;

                client.Credentials = new NetworkCredential(ViewModel.UserAccount.UserName, ViewModel.UserAccount.Password); 
                client.AllowReadStreamBuffering = true;

                // Call and handle the response.
                client.BeginGetResponse(
                    (asResult) =>
                    {
                        Dispatcher.BeginInvoke(
                            () =>
                            {
                                try
                                {
                                    var response = client.EndGetResponse(asResult);
                                    System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
                                    string responseString = reader.ReadToEnd();



                                                                        }
                                catch (WebException failure)
                                {
                                    MessageBox.Show(failure.Message, "Cannot authenticate", MessageBoxButton.OK);
#if DEBUG
                                    throw failure;
#endif
                                }
                            });
                    },
                        null
                );
查看更多
登录 后发表回答