2017-12-05 9 views
0

JavaソースコードをAndroidに変換しようとしています。これは、http POSTを実行してjsonとしてデータを受け取るJavaコードです。それはうまくいく。Retrofit_2 http POST return 403メッセージ

public String httpPost(){ 
String url = "https://webapi.com/api?" 
    List<NameValuePair> params; 
    List<NameValuePair> headers; 

    params.add(new BasicNameValuePair("command", commandValue)); 
    params.add(new BasicNameValuePair("nonce", 
    String.valueOf(System.currentTimeMillis()))); 

    headers.add(new BasicNameValuePair("Key", API_KEY)); 
    headers.add(new BasicNameValuePair("Sign", SECRET_SIGNATURE); 

    HttpPost post = new HttpPost(url); 
     post.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8)); 
     String str = post.getEntity().toString(); 

     if (headers != null) 
     { 
      for (NameValuePair header : headers) 
      { 
       post.addHeader(header.getName(), header.getValue()); 
      } 
     } 

     HttpResponse response = httpClient.execute(post); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) 
     { 
      return EntityUtils.toString(entity); 
     } 

    return null; 
    } 

これはレトロフィット2を使用してAndroidのソースコードである:
APIServiceInterface.java

@POST("api") 
    Call<Mymodel> httpPost(@Header("Key") String key, 
               @Header("Sign") String sign, 
               @Query("command") String command, 
               @Query("nonce") String nonce); 

APIClient.java

public class ApiClient { 
    private static Retrofit retrofit = null; 

    public static Retrofit getClient(String baseUrl) { 
     if (retrofit==null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(baseUrl) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 
     return retrofit; 
    } 
} 

POSTリクエスト

String url = "https://webapi.com/"; 
    APIServiceInterface apiService = ApiClient.getClient(url).create(APIServiceInterface.class); 
    Call<MyModel> call = apiService.httpPost(apiKey, secretSignature, commandValue, String.valueOf(System.currentTimeMillis())); 
      call.enqueue(new Callback<MyModel>() { 
       @Override 
       public void onResponse(Call<MyModel> call, Response<MyModel> response) { 
        int statusCode = response.code(); 
         Log.d(TAG,"http POST:\n[header]:\n" + call.request().headers() + 
        "\n[request]:\n" + call.request().toString() + 
        "\n[body]: \n" + call.request().body().toString()+ 
        "\n----------------------------------------------"+ 
        "\nResponse:\n[header]:\n" + response.headers() + 
        "\n[body]:\n"+ response.body() + 
        "\n[raw]:" + response.raw() + 
        "\n[status] :" + response.code() + 
        "\n[message] :" + response.message()); 
       } 

       @Override 
       public void onFailure(Call<MyModel> call, Throwable t) { 
        // Log error here since request failed 
        Log.e(TAG, "Request failed :" + t.toString()); 
       } 
      }); 

これはAndroidのソースコードは動作しません。しかし、それは常に403メッセージを返し、デバッグログ

http POST: 
[header]: 
Key: MHKVXY9C-9WE8N6CS-L1ETB10V-TFUA4S8G 
Sign: 2534CCB0C58850B4FA621539D5AA5ACB0433B8F47070E6ED13757006F85FF1E4244D3118BDE107C46A7F5587C06BA201F74DE10003330ECBD352757D23E57ACA 

[request]: 
Request{method=POST, url=https://webapi.com/api?command=commandValue&nonce=nonceValue, tag=null} 
[body]: 
[email protected] 
---------------------------------------------- 
Response: 
[header]: 
date: Tue, 05 Dec 2017 07:07:50 GMT 
content-type: text/html; charset=UTF-8 
set-cookie: __cfduid=d0c467a5dbc10a660a569227616baa28f1512457670; expires=Wed, 05-Dec-18 07:07:50 GMT; path=/; domain=.webapi.com; HttpOnly 
cf-chl-bypass: 1 
cache-control: max-age=2 
expires: Tue, 05 Dec 2017 07:07:52 GMT 
x-frame-options: SAMEORIGIN 
server: cloudflare-nginx 
cf-ray: 3c852bb78ea732ef-HKG 

[body]: 
null 
[raw]:Response{protocol=h2, code=403, message=, url=https://webapi.com/api?command=commandValue&nonce=nonceValue} 
[status] :403 
[message] : 

です。 私のコードはどこか間違っていますか?
誰でも助けてくれますか?ありがとう。

+1

は、logcatで完全なエラーを提供します – Aks4125

+0

私はあなたの質問をデバッグログで更新しました。投稿のヘッダーに何か間違っていると思うが、何かが見つからないかもしれないが、まだ分かっていない。 – neo

答えて

0

ベリファイKey & Sign最初にサーバーからのヘッダー。

403禁止されたエラーは、何らかの理由で、アクセスしようとしているページまたはリソースへのアクセスが絶対に禁止されていることを意味するHTTPステータスコードです。

あなたが渡しているコンテンツタイプを確認し、コンテンツタイプがPostmanであるかどうかを確認してください。

郵便配達員でapiが動作しない場合は、Androidで動作させることはできません。


UPDATE

httpClient.addInterceptor(chain -> { 
     Request original = chain.request(); 

     // Request customization: add request headers 
     Request.Builder requestBuilder = original.newBuilder() 
       .header("Key", API_KEY) 
       .header("Sign", SECRET_SIGNATURE); 
       .header("Content-Type", "application/json"); // set your content type 

     Request request = requestBuilder.build(); 
     return chain.proceed(request); 
    }); 

    Retrofit retrofit = builder.client(httpClient.build()).build(); 

ポストAPIがあるべき、HTTPクライアントと一緒にヘッダを渡してみてください

@POST("api") 
Call<Mymodel> httpPost(
         @Query("command") String command, 
         @Query("nonce") String nonce); 
+0

私はPostmanと一緒に試してみました。 ヘッダー: – neo

+0

@neo check update。 – Aks4125

0

私はあなたの資格情報が正しいかもしれないと思うが、あなたのサーバー(webapi)からCloudFlareサーバー(cloudflare-nginx)からエラー403を取得しています。 あなたのご要望は、曇りフラフラを使用して作成する必要があります

関連する問題