【问题标题】:OKHTTP is secure?OKHTTP 安全吗?
【发布时间】:2016-04-27 17:33:34
【问题描述】:

我正在构建一个需要将用户登录到我们的服务器并且连接需要安全 (HTTPs) 的 android 应用程序。我的问题是,我应该为此目的使用 OKHTTP 库吗?

我使用该库来记录一个用户,如下所示:

public class MainActivity extends AppCompatActivity {
public static final MediaType JSON
        = MediaType.parse("application/json; charset=utf-8");
private static final String TAG = MainActivity.class.getSimpleName();
private JSONObject responseJson;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    final JSONObject myJson = new JSONObject();
    try {
        myJson.put("udid","c376e418-da42-39fb-0000-d821f1fd2804");
        myJson.put("email","email
        myJson.put("password","password");
    } catch (JSONException e) {
        e.printStackTrace();
    }

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    //Your code goes here
                   String response =  post("https://ADDRESS/v1/auth", myJson.toString());
                    responseJson = new JSONObject(response);
                    String message = responseJson.getString("message");
                    String token = responseJson.getString("token");
                    Log.d(TAG,"Response message: " + message);
                    Log.d(TAG,"Response token: " + token);
                    Log.d("MainActivity",response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

}

String post(String url, String json) throws IOException {
    OkHttpClient client = new OkHttpClient();
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}
}

【问题讨论】:

  • 如果您想进一步锁定证书,请查看证书固定。这很危险,因为您可以将自己锁定,但它可以保护您免受本地设备上的一些安全问题。

标签: android https okhttp


【解决方案1】:

我应该为此目的使用 OKHTTP 库吗?

OkHttp 支持https URL,与大多数 Android 的 HTTP 客户端库一样。

【讨论】:

  • 感谢您的回答。但是,我仍然不明白我使用它的方式是否安全(也就是使用 HTTPs 发布请求)
  • @Rohit5k2 谢谢你的回答!我希望听到我喜欢使用该库是多么简单,并且不想自己经历实现 HttpsURLConnection 的麻烦:p
  • 我在我的一个与真钱有关的项目中使用OkHttp。相信我它的工作完美。虽然我已经根据我的具体要求对其进行了一些其他的定制。
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 2018-04-06
  • 2015-05-04
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 2010-11-20
  • 2019-12-07
相关资源
最近更新 更多