2017-08-04 68 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();