HttpWebResponse错误,未找到

时间:2011-07-02 13:19:36

标签: c# silverlight windows-phone-7

我有一个使用HttpWebRequest的奇怪问题,我正在尝试将字符串发布到服务但是HttpWebResponse不断产生以下错误;

"System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)\r\n   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)\r\n   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)\r\n   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)\r\n   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)\r\n   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)\r\n   at System.Delegate.DynamicInvokeOne(Object[] args)\r\n   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)\r\n   at System.Delegate.DynamicInvoke(Object[] args)\r\n   at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()\r\n   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)\r\n   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)\r\n   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)\r\n   at System.Delegate.DynamicInvokeOne(Object[] args)\r\n   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)\r\n   at System.Delegate.DynamicInvoke(Object[] args)\r\n   at System.Windows.Threading.DispatcherOperation.Invoke()\r\n   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)\r\n   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)\r\n   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)\r\n   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)\r\n   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)\r\n\r\n   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)\r\n   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n   at ZabbixClient.MainPage.ResponseCallBack(IAsyncResult result)\r\n   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)\r\n   at System.Threading.ThreadPool.WorkItem.doWork(Object o)\r\n   at System.Threading.Timer.ring()\r\n"

我的代码看起来像;

 private void btnSignin_Click(object sender, RoutedEventArgs e)
    {
        // Prepare web request...
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://monitor.co.uk", UriKind.Absolute));
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.BeginGetRequestStream(new AsyncCallback(RequestCallBack), myRequest);
    }

void RequestCallBack(IAsyncResult result) {
        HttpWebRequest myRequest = result.AsyncState as HttpWebRequest;

        //need error checking for this part
        Stream stream = myRequest.EndGetRequestStream(result);

    using (StreamWriter sw = new StreamWriter(stream)){

        sw.Write("{ \"jsonrpc\":\"2.0\",\"method\":\"user.authenticate\",\"params\":{\"user\":\"<login>\",\"password\":\"<password>\"},\"id\":2}");
    }
    myRequest.BeginGetResponse(ResponseCallBack, myRequest);
    }

void ResponseCallBack(IAsyncResult result)
    {
        //get to the request object
        HttpWebRequest myRequest = result.AsyncState as HttpWebRequest;
        try
        {
            //need error checking here
            HttpWebResponse response = myRequest.EndGetResponse(result)
                as HttpWebResponse;
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(sr.ReadToEnd()); });
            }
        }
        catch (WebException webExcp)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(webExcp.ToString()); });
        }
    }

我只是无法弄清楚发生了什么,URL被正确指定并正常工作,我读到使用小提琴来监控正在发生的事情,但是小提琴手中没有任何内容表明它甚至没有提出请求?任何信息,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:3)

首先,让我指出代码中的问题:

using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
   System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(sr.ReadToEnd()); });
}

在您尝试显示结果时,流将关闭。你应该做的是这样的事情:

using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    String s = sr.ReadToEnd();
    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(s); });
}

然而,我不确定你为什么要在MessageBox实例中显示响应 - 它基本上是不可读的 - 使用Output控制台进行调试。

回到主题 - NotFound通常由服务器返回,与操作系统正在处理的请求无关。这是一个非常通用的错误,您需要确保在另一端支持您正在调用的内容。

确保您有良好的互联网连接(在旁注上)。

答案 1 :(得分:1)

我遇到了同样的问题。

我有一个代理服务器,问题从这里开始。我启动了模拟器,然后继续启用和禁用代理服务器。我发现当说明模拟器时,它会保留代理配置,即使您更改了代理,它也始终保持正式配置。 然后,我禁用了代理,启动了模拟器,我的应用程序运行良好。 Windows Phone 7.1 httpWebRequest对代理无法正常工作。使用Windows Phone 7 httpWebRequest我没有遇到同样的问题。我将Windows Phone 7应用程序转换为Windows Phone 7.1后,我遇到了这个问题。

希望它可以帮到你