我在使用 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 中没有任何内容表明它甚至没有发出请求?任何信息,将不胜感激。谢谢!
请您参考如下方法:
首先,让我指出您代码中的一个问题:
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 通常由服务器返回,与操作系统正在处理的请求无关。这是一个非常普遍的错误,您需要确保您所调用的内容在另一端得到支持。
确保您的互联网连接良好(附注)。
