2011-07-02 42 views
1

我有一個奇怪的問題,使用HttpWebRequest,我想發佈一個字符串到服務,但HttpWebResponse不斷產生以下錯誤;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()); }); 
     } 
    } 

我只是無法弄清楚是怎麼回事,該網址是正確指定和工作,我看用小提琴來監控發生了什麼事情,但沒有出現在提琴手暗示它甚至沒有獲得做出請求?任何信息,將不勝感激。謝謝!

+0

我剛纔注意到您發送JSON數據,但設置請求的ContentType標頭爲「application/X WWW的形式,進行了urlencoded」。這是服務器期望的嗎? –

回答

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通常由服務器返回,與操作系統正在處理的請求無關。這是一個非常普遍的錯誤,您需要確保您所調用的內容在另一端受到支持。

確保您有良好的互聯網連接(在附註上)。

+0

感謝您的回覆,我只是爲了調試的原因使用了messagebox。唯一的錯誤,我可以看到在輸出位是; mscorlib.dll中發生類型'System.IO.FileNotFoundException'的第一次機會異常 System.Windows.dll中發生類型'System.Net.WebException'的第一次機會異常 類型'System.Net .WebException'發生在System.Windows.dll – Nathan

+0

我剛剛在新項目中使用了您的代碼(如上所示)。沒有出現這樣的錯誤。您可能會在項目中進行其他操作,以防止正確執行(如其他例外情況所示)。在請求中 - 您確定服務器可以處理您的請求嗎? –

+0

我正在使用Windows Phone SDK 7.1版Beta 2。您運行的是哪個版本? – Nathan

1

我有同樣的問題。

我有一臺代理服務器,問題從這裏開始。我啓動了模擬器,然後繼續啓用和禁用代理服務器。我發現,當模擬器被聲明時,它會保持代理配置,即使您更改代理,它也會始終保持最初的配置。 然後,我禁用了代理,啓動了模擬器,並且我的應用程序完美運行。 Windows Phone 7.1 httpWebRequest對於代理無法正常工作。我沒有使用Windows Phone 7 httpWebRequest的相同問題。在將Windows Phone 7應用程序轉換爲Windows Phone 7.1後,我遇到了這個問題。

希望它可以幫助你