HttpWebResponse错误,找不到

本文关键字:找不到 错误 HttpWebResponse | 更新日期: 2023-09-27 18:01:01

我在使用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指定正确且有效,我读到用fiddle来监控发生了什么事情,但fiddler中没有任何内容表明它甚至无法发出请求?如有任何信息,我们将不胜感激。谢谢

HttpWebResponse错误,找不到

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

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通常由服务器返回,与操作系统处理的请求无关。这是一个非常常见的错误,您需要确保您正在调用的内容在另一端得到支持。

确保你有一个良好的互联网连接(附带说明(。

我也遇到了同样的问题。

我有一个代理服务器,问题从这里开始。我启动了模拟器,然后不断地启用和禁用代理服务器。我发现,当模拟器被声明时,它会保留代理配置,即使你更改了代理,它也总是保留初始配置。然后,我禁用了代理,启动了模拟器,我的应用程序运行得很好。Windows Phone 7.1 httpWebRequest无法与代理一起正常工作。我在使用Windows Phone 7 httpWebRequest时没有遇到同样的问题。我刚刚在将我的Windows Phone 7应用程序转换为Windows Phone 7.1后遇到了这个问题。

希望它能帮助你