2011-07-20 46 views
0

我需要使用POST發送一個字符串到服務器並獲得xml響應,狀態碼是OK,但字符串響應總是=「」(0字節)。我的代碼有什麼問題嗎?我檢查從黑莓,正常工作的服務器,所以這個問題必須來自我的代碼:httpWebRequest使用POST方法 - 接收意外的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(); 
    } 

*編輯** 的代碼工作,但服務器返回的參數要求和號(&)的我認爲不允許在silverlight框架中,刪除& char服務器響應,但結果不正確。我會問一個新的問題,請參考此&符號

+0

可能值得檢查成功的rcode。 –

回答

0

帖子值應該是「track = love」。

我試過了,但它的工作原理,但迴應是gzip編碼。

1

已經使用Fiddler檢查了您的呼叫。服務器正在返回一個空的主體。你的代碼工作正常。問題在於服務器端。

0

Fiddler說這個請求很好,而且響應真的是空白。如果它使用的是Blackberry而不是WP7,那麼服務器是否可以進行一些用戶代理檢查,並且不返回任何內容,因爲它不能識別WP7用戶代理?

此外,它看起來像你張貼查詢字符串(「?& track =愛」),這是不尋常的。您確定這是您的請求中發送的正確數據嗎?