2017-10-25 7 views
0

実行しているローカルサーバーに対してGETリクエストを作成しようとしています。私は正しいデータを返すのに苦労しています。私は「Unauthorized」という応答を見ています。文字列 'トークン'が正しいことを考えると、誰でもこれで目立った問題を発見することはできますか?トークン認証を使用したJava HTTPリクエスト

protected Object doInBackground(Void... params) { 
     try { 
      String url = "http://192.168.0.59:8000/events/"; 
      URL object = new URL(url); 
      HttpURLConnection con = (HttpURLConnection) object.openConnection(); 
      con.setDoOutput(true); 
      con.setDoInput(true); 
      con.setRequestMethod("GET"); 
      con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
      con.setRequestProperty("Accept", "application/json"); 
      con.setRequestProperty("Authorization:", "Token " + token); 

      //Display what the GET request returns 
      StringBuilder sb = new StringBuilder(); 
      int HttpResult = con.getResponseCode(); 
      if (HttpResult == HttpURLConnection.HTTP_OK) { 
       BufferedReader br = new BufferedReader(
         new InputStreamReader(con.getInputStream(), "utf-8")); 
       String line = null; 
       while ((line = br.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       br.close(); 
      } else { 
       System.out.println(con.getResponseMessage()); 
      } 
     } catch (Exception e) { 
      Log.d("Uh Oh","Check your network."); 
      return false; 
     } 
     return false; 

} *

私は、コマンドラインから作業カール要求を取得することができた:

curl -H "Authorization: Token token" http://0.0.0.0:8000/events/ 
+0

あなた自身を認証する必要があります。 – atom

+0

https://stackoverflow.com/questions/12732422/adding-header-for-httpurlconnectionを参照してください –

+1

':'を '' Authorization: "'にドロップしてください。 –

答えて

1

それはGETリクエストがないと、この問題はcon.setDoOutput(true);含むことによって引き起こされた判明体を含む。

関連する問題