2011-07-20 8 views
0

POSTを使用してサーバーに文字列を送信し、XML応答を取得する必要がありますが、ステータスコードはOKですが、文字列の応答は常に ""(0バイト)です。私のコードに間違いがありますか?私は、ブラックベリーからサーバーをチェックし正常に動作しますので、問題は私のコードから来なければならない:POSTメソッドを使用したhttpWebRequest - 予期しないxml応答を受け取ります

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     s = NavigationContext.QueryString["parameter1"]; 
     //string strConnectUrl = "http://www.contoso.com/example.aspx"; 
     base.OnNavigatedTo(e); 
    //  DoWebClient(s); 
     try 
     { 

      HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strConnectUrl); 
      httpWebRequest.Method = "POST"; 
      httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; 
      // start the asynchronous operation 
      httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpWebRequest); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 

    private static void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 
     // string XML_REQUEST = "<?xml version=\"1.0\"?><mybroker"><getConnections></mybroker>"; 
     string post = "?&track=love"; 

     try 
     { 
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

      // End the operation 
      Stream postStream = request.EndGetRequestStream(asynchronousResult); 

      // Convert the string into a byte array. 
      byte[] postBytes = Encoding.UTF8.GetBytes(post); 

      // Write to the request stream. 
      postStream.Write(postBytes, 0, postBytes.Length); 
      postStream.Close(); 

      // Start the asynchronous operation to get the response 
      request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
    static Stream str; 
    static string st; 
    private static void GetResponseCallback(IAsyncResult asynchronousResult) 
    { 

      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

      // End the operation 
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
      HttpStatusCode rcode = response.StatusCode; 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamRead = new StreamReader(streamResponse); 

     //****THIS ALWAYS RETURN "" VALUE, EXPECT TO RETURN XML STRING**** 
      string responseString = streamRead.ReadToEnd(); 

      //Console.WriteLine(responseString); 
      // Close the stream object 
      streamResponse.Close(); 
      streamRead.Close(); 
      // Release the HttpWebResponse 
      response.Close(); 
    } 

* EDIT ** コードの仕事、しかし、サーバーの戻りパラメータはアンパサンド(&)を必要とします私は思うSilverlightフレームワークで許可されていません& charサーバーの応答を削除しますが、結果が正しくありませんでした。私は新しい質問をします。このアンパサンドを参照してください

+0

成功のためにrcodeをチェックする価値があります。 –

答えて

0

投稿値は「track = love」である必要があります。

私は試してみましたが、うまくいきましたが、応答はgzipでエンコードされています。

1

あなたの呼び出しをFiddlerでチェックしましたか?サーバーが空の本体を戻しています。コードが正しく動作しています。問題はサーバー側です。

0

Fiddlerは、リクエストが良好で、応答が本当に空であると言います。それがWP7からではなく、ブラックベリーから作業している場合、サーバーはWP7ユーザーエージェントを認識しないため、何らかのユーザーエージェントチェックを行い、何も返さない可能性がありますか?

また、珍しいことであるクエリ文字列( "?& track = love")を投稿しているようです。あなたの要求に送信するのが正しいデータだと確信していますか?

関連する問題