2016-05-02 5 views
1

Ich erstelle eine Verbindung zu einem Authentifizierungsserver (OAuth 2.0), um ein Token zu erhalten. Das Verfahren, das dies tut, ist die folgende:URLConnection beendet den Empfang von InputStream nicht

public static async Task<String> GetToken(string username, string password) 
     { 
      String response = null; 

      await Task.Run(() => 
      { 
       URL url = new URL(Configuration.Configuration.baseURL + Configuration.Configuration.tokenPath); 
       HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection(); 
       urlConnection.DoOutput = true; 
       urlConnection.DoInput = true; 
       urlConnection.RequestMethod = "POST"; 
       urlConnection.SetRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

       var content = new FormUrlEncodedContent(new[] 
       { 
        new KeyValuePair<string, string>("grant_type", "password"), 
        new KeyValuePair<string, string>("username", username), 
        new KeyValuePair<string, string>("password", password), 
        new KeyValuePair<string, string>("client_id", "widas_servicebar"), 
        new KeyValuePair<string, string>("scope", Configuration.Configuration.scopes) 
       }); 

       //var content = new Dictionary<string, string>(); 
       //content.Add("grant_type", "password"); 
       //content.Add("username", username); 
       //content.Add("password", password); 
       //content.Add("client_id", "widas_servicebar"); 
       //content.Add("scope", Configuration.Configuration.scopes); 

       using (var streamWriter = new StreamWriter(urlConnection.OutputStream)) 
       { 
        streamWriter.Write(content); 
        streamWriter.Flush(); 
        streamWriter.Close(); 
       } 

       urlConnection.Connect(); 
       var stream = urlConnection.InputStream; 
       using (var streamReader = new StreamReader(stream)) 
       { 
        response = streamReader.ReadToEnd();      
        return response; 
       } 
      }); 
      return response; 
     } 

Leider, wenn ich den Code debuggen es wird bei var stream = urlConnection.InputStream; und von dort auf nichts passiert mehr immer stecken (Ich habe versucht, 15 Minuten warten und mit Postman der Anforderung erhält eine Antwort in wie 200-500ms).

Ich benutzte fast den gleichen Code für einige andere Methoden, die auch eine Verbindung zu einem Server (Content-Server) bauen und diese ordnungsgemäß funktionieren.

Gibt es einen Fehler in meinem Code oder was könnte der Grund dafür sein, dass der Input-Stream nie beendet wird?

Wenn es von Bedeutung ist: Ich baue ein Xamarin.Android-Projekt.

+0

Es gibt einen weiteren ähnlichen Thread in SO, sorry wenn seine URL jetzt nicht finden konnten, eine der Höhe gestimmt Antwort (nicht akzeptiert) besagt, dass das Problem nur auftritt, wenn sie einen Unterbrechungspunkt auf die Anweisung setzen, wo sie auf den Eingabestream zugreifen :) In Ihrem Fall var streamWriter = new StreamWriter (urlConnection.OutputStream) Wenn Sie also einen Unterbrechungspunkt setzen, entfernen Sie es und legte es auf Rückantwort; überprüfe, ob Antwort da ist oder nicht :) Ich hatte das selbe Problem tat, was ich sagte, es funktionierte, ich weiß, klingt etwas verrückt, aber einen Versuch wert, lemme wissen, ob es funktioniert :) bis dann lemme den Link finden :) –

+0

Es tat es nicht Arbeit:/Wäre toll, wenn Sie den Link finden würden. Ich werde auch im Internet nach mehr suchen, um eine Antwort zu finden. – fbueckle

Antwort

0

Ich habe es zu arbeiten. Es scheint so, als ob das, was ich versuchte zu benutzen, nicht so funktionierte, wie ich es dachte. die jetzt arbeiten Code sieht wie folgt aus:

public static async Task<String> GetToken(string username, string password) 
     { 
      String response = null; 

      await Task.Run(() => 
      { 
       URL url = new URL(Configuration.Configuration.baseURL + Configuration.Configuration.tokenPath); 
       HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection(); 
       urlConnection.DoOutput = true; 
       urlConnection.DoInput = true; 
       urlConnection.RequestMethod = "POST"; 
       urlConnection.SetRequestProperty("Content-Type", "application/x-www-form-urlencoded");        

       var content = new Dictionary<string, string>(); 
       content.Add("grant_type", "password"); 
       content.Add("username", username); 
       content.Add("password", password); 
       content.Add("client_id", "widas_servicebar"); 
       content.Add("scope", Configuration.Configuration.scopes); 

       Stream os = urlConnection.OutputStream; 
       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
       writer.Write(getQuery(content)); 
       writer.Flush(); 
       writer.Close(); 
       os.Close(); 

       urlConnection.Connect(); 
       //var debug = urlConnection.ErrorStream; 
       var stream = urlConnection.InputStream; 

       using (var streamReader = new StreamReader(stream)) 
       { 
        response = streamReader.ReadToEnd();      
       } 
       return response; 
      }); 
      return response; 
     } 

Die getQuery Methode, dies wie folgt aussieht:

private static String getQuery(Dictionary<string, string> dictionary) 
     { 
      StringBuilder result = new StringBuilder(); 
      bool first = true; 

      foreach (KeyValuePair<string, string> entry in dictionary) 
      {  
       if (first) 
       { 
        first = false; 
       }   
       result.Append(URLEncoder.Encode(entry.Key, "UTF-8")); 
       result.Append("="); 
       result.Append(URLEncoder.Encode(entry.Value, "UTF-8")); 
       result.Append("&"); 
      } 

      return result.ToString(); 
     }