I have a strange issue using HttpWebRequest, I’m trying to post a string to a service but HttpWebResponse keeps producing the following error;
"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"
My code looks like;
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()); });
}
}
I just can’t figure out what’s going on, the URL is specified correctly and working, I read to use fiddle to monitor what was going on but nothing appears in fiddler suggesting it’s not even getting to make a request? Any info would be appreciated. Thanks!
First, let me point out a problem in your code:
The stream will be closed by the time you will attempt to display the result. What you should do is have something like this:
Yet, I am not sure why you want to display the response in a
MessageBoxinstance – it will be basically unreadable – use theOutputconsole for debugging purposes.Back on topic –
NotFoundis usually returned by the server and has nothing to do with the request being processed by the OS. It is a very generic error and you need to make sure that what you are invoking is supported on the other end.Make sure that you have a good Internet connection (on a side note).