簡體   English   中英

通過POST將JSON格式的作業應用程序發送到REST API:獲取SSL異常

[英]Sending a JSON formatted job application to a REST API via POST: getting an SSL exception

語境:

我想向公司提交工作申請,有關工作說明發布在此處(“通過我們的REST API申請”): https : //3sidecube.com/contact/join-us

我做了什么:

我已經編寫了一個簡單的應用程序來發送應用程序,它使用“ Android異步HTTP客戶端”庫來完成大部分工作: http : //loopj.com/android-async-http/

這是該應用程序的代碼:

主要活動:

package jobsrc;
import java.io.UnsupportedEncodingException;

import org.apache.http.Header;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.jobapp.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.loopj.android.http.*;


public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        Button b = (Button) findViewById(R.id.btnSend);
        b.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                sendRequest();
            }
        });     
    }

    private void sendRequest() {
        final String TAG = "jobApp";
        Log.i(TAG, "Sending post..");   

        AsyncHttpClient client = new AsyncHttpClient();
        JSONObject params = new JSONObject();
//      //fill params here..
        try {
            params.put("name", "TEST");     
            params.put("email", "TEST");
            params.put("message", "TEST");
            params.put("cv-link", "TEST");
            params.put("github-profile", "TEST");
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        StringEntity se = null;
        try{
            se = new StringEntity(params.toString());
        }catch(UnsupportedEncodingException e){

        }

        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        //DEBUG
        //requestbin test: ("http://requestb.in/18jj7lk1?inspect" to see result)
        //client.post(null, "http://requestb.in/18jj7lk1", se, "application/json", new JsonHttpResponseHandler(){

        //submission URL: transmission fails with error: 
        //  "javax.net.ssl.SSLException: hostname in certificate didn't match: <3sidedcube.com> != <*.cubeapis.com> OR <*.cubeapis.com> OR <cubeapis.com>"
        client.post(null, "https://3sidedcube.com/api/jobs", se, "application/json", new JsonHttpResponseHandler(){

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                    JSONArray response) {
                Log.i(TAG, "Success: array");
                Log.i(TAG, response.toString());
                super.onSuccess(statusCode, headers, response);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                    JSONObject response) {
                Log.i(TAG, "Success: object");
                Log.i(TAG, response.toString());
                super.onSuccess(statusCode, headers, response);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                    String responseString) {
                Log.i(TAG, "Success: string");
                Log.i(TAG, responseString);
                super.onSuccess(statusCode, headers, responseString);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                    String responseString, Throwable throwable) {
                // TODO Auto-generated method stub
                super.onFailure(statusCode, headers, responseString, throwable);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                    Throwable throwable, JSONArray errorResponse) {
                // TODO Auto-generated method stub
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                    Throwable throwable, JSONObject errorResponse) {
                // TODO Auto-generated method stub
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }

        });

    }
}

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Send.." />

</RelativeLayout>

問題:

運行代碼時,我收到一個onFailure響應,該響應正在接收SSL異常,調試將異常顯示為:

"javax.net.ssl.SSLException: hostname in certificate didn't match: <3sidedcube.com> != <*.cubeapis.com> OR <*.cubeapis.com> OR <cubeapis.com>"

我需要知道的是:

這是我的錯,還是他們的錯? 我不是這種事情的專家,所以我很可能在做傻事!

我使用RequestBin(“ http://requestb.in/18jj7lk1?inspect ”)測試了POST請求,在ECHO看來它是正確而准確的,所以我覺得這可能是他們的問題?

無論如何,如果是我的錯,我將想糾正自己在做什么,如果是他們的錯,我會給他們發送電子郵件並向他們解釋問題。

讓我知道您的想法,謝謝您的幫助!

3sidecube.com需要使用准確的域(CN)修復其證書,或者您需要禁用主機名驗證。 您可以使用下面給出的代碼繞過驗證。

  ....
  KeyStore trustStore=KeyStore.getInstance(KeyStore.getDefaultType());
  trustStore.load(null,null);
  SSLSocketFactory socketFactory=new InsecureSSLSocketFactory(trustStore);
  socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  client.setSSLSocketFactory(socketFactory);
  client.post(null, "https://3sidedcube.com/api/jobs", se, "application/json", new         JsonHttpResponseHandler(){
  .....

我是3 Sided Cube的開發人員之一。 感謝您發現我們的API故意錯誤;-)

如果您現在在https://3sidecube.com/api/job上嘗試您的請求,您應該會得到更有用的答復。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM