2011-07-02 30 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()); }); 
     } 
    } 

、URLが正しく指定され、動作して、私は何が起こっていたかを監視するためにバイオリンを使用するように読まが、何もそれも作ることになっていない示唆シオマネキに表示されません要求?すべての情報は高く評価されます。ありがとう!

+0

役立つことを願って。これはサーバーが期待しているものですか? –

答えて

3

まず、私はあなたのコードで問題を指摘してみましょう:

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

ストリームは、あなたが結果を表示しようとする時間によって閉じられます。あなたはMessageBoxインスタンス内の応答を表示したい私はなぜわからない、

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

しかし - それは基本的に読めなくなります - デバッグ目的Outputコンソールを使用:あなたがすべきことは、このようなものを持っています。

トピックに戻る - NotFoundは、通常、サーバーによって返され、OSによって処理されている要求とは関係ありません。これは非常に一般的なエラーで、呼び出すものが相手側でサポートされていることを確認する必要があります。

インターネット接続が良好であることを確認してください(別紙)。

+0

あなたの返信をありがとう、私はちょうどデバッグの理由のためにメッセージボックスを使用していた。 Outputビットに表示される唯一のエラーは次のとおりです。 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

私は同じ問題がありました。

プロキシサーバーがあり、問題がここから始まります。エミュレータを起動してから、プロキシサーバーの有効化と無効化を続けました。私はエミュレータがプロキシの設定を保持していることを確認すると、たとえプロキシを変更しても常にinicial設定を保持していることが分かります。 その後、私はプロキシを無効にし、エミュレータを起動してアプリケーションが正常に動作しました。 Windows Phone 7.1のhttpWebRequestは、プロキシで正常に動作しません。私はWindows Phone 7のhttpWebRequestを使用して同じ問題を抱えていませんでした。 Windows Phone 7アプリケーションをWindows Phone 7.1に変換した後、この問題が発生しました。

はそれが私はちょうどあなたがJSONデータを送信しますが、「アプリケーション/ x-www-form-urlencodedで」と要求のContentTypeヘッダーを設定していることに気付きましたあなた

関連する問題