0

我試圖從WP7應用程序調用google URL shortner API,我第一次嘗試在控制檯應用程序通過這樣做:HttpWebRequest的「POST」返回服務器錯誤(異常)

 var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url"); 
     httpWebRequest.ContentType = "application/json"; 
     httpWebRequest.Method = "POST"; 

     using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) 
     { 
      string json = "{\"longUrl\":\"http://www.google.com/\"}"; 
      Console.WriteLine(json); 
      streamWriter.Write(json); 
     } 

     var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 
     using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
     { 
      var responseText = streamReader.ReadToEnd(); 
      Console.WriteLine(responseText); 
     } 
     Console.Read(); 

,它工作得很好,回到一切就OK了,但是當我嘗試做一個的Windows Phone應用程序這樣的:

private void button1_Click(object sender, RoutedEventArgs e) 
    {    
     testConnection(); 
    } 

    private void testConnection() 
    { 
     if (!NetworkInterface.GetIsNetworkAvailable()) 
      MessageBox.Show("There's no internet connection, please reconnect to the internet and try again"); 
     else 
     {     
      var req = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url"); 
      req.ContentType = "application/json"; 
      req.Method = "POST"; 
      req.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), req); 
      textBlock2.Text = "Done";    

     } 
    } 
    void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
     // End the stream request operation 
     Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); 

     // Create the post data 
     // Demo POST data: 
     string postData = "http://www.google.com"; 
     byte[] byteArray = Encoding.Unicode.GetBytes(postData); 

     // Add the post data to the web request    
     postStream.Write(byteArray, 0, byteArray.Length); 
     postStream.Close(); 

     // Start the web request 
     webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 
    } 


    void GetResponseCallback(IAsyncResult asynchronousResult) 
    { 

     try 
     { 
      HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
      HttpWebResponse response; 
      // End the get response operation 
      response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamReader = new StreamReader(streamResponse); 
          textBlock1.Text= streamReader.ReadToEnd(); 
      streamResponse.Close(); 
      streamReader.Close(); 
      response.Close(); 

     } 
     catch (WebException e) 
     { 

     } 
    } 

代碼總是轉到試方法的漁獲物和給出「未找到」錯誤。

+0

後滿異常消息,和堆棧跟蹤。 –

回答

1

存在SSL和不可信證書的已知問題。當前的Windows Phone 7應用程序模型不支持使用需要ClientCertificates的SSL連接網站(僅支持BasicAuthentication)。你可以在這裏閱讀關於同樣的問題:http://forums.create.msdn.com/forums/p/65076/398730.aspx

+0

我已經嘗試過與另一個沒有https,我厭倦了與http相同的鏈接,同樣的問題發生 – SKandeel

0

我有一個類似的問題(這是有道理的,因爲我開始與這個代碼作爲我的例子...),當我改變下面的代碼,它開始工作到點我可以再次前進。

...來自:

byte[] byteArray = Encoding.Unicode.GetBytes(postData); 

...到(相當於):

byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
相關問題