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

ポストフル例外メッセージ、およびスタックトレース:(相当)に

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

...:から

...。 –

答えて

1

SSLおよび信頼できない証明書には既知の問題があります。 ClientCertificatesが必要なSSLを使用したWebサイトへの接続は、現在のWindows Phone 7アプリケーションモデルではサポートされていません(BasicAuthenticationのみがサポートされています)。 http://forums.create.msdn.com/forums/p/65076/398730.aspx

+0

私はhttpsなしでもう1つ試してみましたが、httpと同じリンクに疲れました。同じ問題が発生します – SKandeel

0

私はこのコードを私の例として始めて以来、似たような問題がありました。私が次のコードを変更したとき、それはポイントに作業を開始しましたそこで私は再び前進することができました。

byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
関連する問題