2017-08-04 80 views
0

我喜歡從網站獲取HTML。 此網站有認證。 認證是這樣的:OkHTTP3無法通過驗證android

1)用戶輸入用戶名和密碼的身體發送POST請求 2)服務器檢查用戶身體 3)如果用戶名和密碼都OK,服務器Location標頭中發送302與ADRESS用於重定向,其中我可以得到HTML 4)用戶發送POST鏈接Location並獲得HTML。 5)如果用戶名和密碼錯誤的服務器也發送302,但在Location其他地址用表單進行身份驗證。

我在JavaScript上有一個代碼,它的工作很好! 我想在Android上使用OkHTTP 3.8.1來做同樣的事情,但我不能,所有的時間我都會得到'200選項。這是精我的JavaScript代碼:

var data = "auth-type=mo&username=SECRET&password=SECRET"; 
var xhr = new XMLHttpRequest(); 
xhr.withCredentials = true; 
xhr.addEventListener("readystatechange", function() { 
if (this.readyState === 4) { 
console.log(this.responseText); 
} 
}); 
xhr.open("POST", "https://lk.mango-office.ru/auth"); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 
xhr.send(data); 

這裏是我的錯OkHTPP代碼:

public class MainActivity extends AppCompatActivity 
{ 
    public String postUrl = "https://lk.mango-office.ru/auth"; 
    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     try 
     { 
      postRequest(postUrl, "auth-type=mo&username=SECRET&password=SECRET"); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    void postRequest(String postUrl, String postBody) throws IOException 
    { 
     String str = postBody; 
     OkHttpClient client = new OkHttpClient(); 
     RequestBody body = RequestBody.create(mediaType, postBody); 
     Request request = new Request.Builder() 
      .url("https://lk.mango-office.ru/auth") 
      .post(body) 
       .addHeader("Content-Type", "application/x-www-form-urlencoded") 
       .build(); 

     client.newCall(request).enqueue(new Callback() 
     { 
      @Override 
      public void onFailure(Call call, IOException e) 
      { 
       call.cancel(); 
      } 

      @Override 
      public void onResponse(Call call, Response response) throws IOException 
      { 
       Log.d("TAG", response.body().string()); 
      } 
     }); 
    } 
} 

回答

0

postbody應該是這樣的

JSONObject body = new JSONObject(); 

body.put("auth-type", "mo"); 
body.put("username", "SECRET"); 
body.put("password", "SECRET"); 

然後用「身體代替 「postbody」 「的jsonobject

RequestBody body = RequestBody.create(mediaType, body.toString()); 

然後取出

.addHeader("Content-Type", "application/x-www-form-urlencoded") 
+0

@Vryin對不起,我不明白。 U說我必須創建JSONObject,它可以。然後U說我必須將這個JSONObject傳遞給RequestBody.create()。但是我得到一個errod,導致RequestBody.create()不能使用JSONObject。 – Masquitos

+0

對不起,我會更新答案。 – Bryan

+0

@Vryin現在編譯它,但仍然不能正常工作,如JavaScript代碼 – Masquitos

0

你可以試試下面的代碼授權:

OkHttpClient client = new OkHttpClient.Builder() 
       .addInterceptor(new Interceptor() { 
        @Override 
        public Response intercept(Chain chain) throws IOException { 
         Request original = chain.request(); 

         String credentials = "username" + ":" + "password"; 
         final String basic = 
           "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); 
         Request.Builder requestBuilder = original.newBuilder() 
           .header("Authorization", basic); 

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