1

そのため、私のサイトでは、いくつかの異なる SSL 証明書を使用しています。1 つはルート ドメイン「illution.dk」用で、もう 1 つはサブドメイン「ci.illution.dk」用です。問題は、HttpPost を使用して投稿リクエストを発行し、「https://ci.illution.dk/login/device」のような URL をリクエストすると、次のようなエラー メッセージがスローされることです。

10-04 18:35:13.100: W/System.err(1680): javax.net.ssl.SSLException: hostname in certificate didn't match: <ci.illution.dk> != <www.illution.dk> OR <www.illution.dk> OR <illution.dk>

これは、illution.dk の証明書をダウンロードし、ci.illution.dk をサポートしていないことを意味していると思います。ただし、ブラウザを起動して「https://ci.illution.dk」を参照すると、すべて問題ありません。私のAndroidコードは次のとおりです。

HttpClient httpclient = new DefaultHttpClient();
        //appContext.getString(R.string.base_url)
        HttpPost httppost = new HttpPost("https://ci.illution.dk/login/device");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", params[0]));
            nameValuePairs.add(new BasicNameValuePair("password", params[1]));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            return response;
        } catch (ClientProtocolException e) {
            Log.d("ComputerInfo", "Error while loggin in: ClientProtocolException");
            return null;
        } catch (IOException e) {
            Log.d("ComputerInfo", "Error while loggin in: IOException");
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            Log.d("ComputerInfo", "Error while loggin in");
            e.printStackTrace();
            return null;
        }
4

2 に答える 2

0

次のコードを見るだけで答えが得られます。私は自分のコードでそれを使用しました。コードで使用する必要があります。

BufferedReader reader = new BufferedReader(new InputStreamReader(is、 "iso-8859-1")、8);

StringBuilder sb = new StringBuilder();

文字列行=null;

while((line = reader.readLine())!= null){

sb.append(line + "\ n");}

近いよ();

私が使用した次の例を見てください

    ArrayList<DailyExpDto> list = new ArrayList<DailyExpDto>();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("date", "" + date));
    qparams.add(new BasicNameValuePair("uid", ""
            + Myapplication.getuserID()));

    try {
        HttpClient httpclient = new DefaultHttpClient();
                    httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(null, -1),
                new UsernamePasswordCredentials("YOURUSRNAME", "YOURPASSWORD"));
        HttpPost httppost = new HttpPost(url + "daily_expenditure.php?");
        httppost.setEntity(new UrlEncodedFormEntity(qparams));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    Log.v("log", result);
    JSONObject jobj = null;
    try {
        jobj = new JSONObject(result);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    try {

        JSONArray JArray_cat = jobj.getJSONArray("category");
        JSONArray JArray_desc = jobj.getJSONArray("description");
        JSONArray JArray_exp = jobj.getJSONArray("expenditure");
        for (int i = 0; i < JArray_cat.length(); i++) {
            DailyExpDto dto = new DailyExpDto();
            dto.category = JArray_cat.getString(i);
            dto.desc = JArray_desc.getString(i);
            dto.exp = JArray_exp.getInt(i);
            list.add(dto);
        }

    } catch (Exception e) {
        // TODO: handle exception
    }
    return list;
}
于 2012-10-04T17:00:57.457 に答える
0

ここにリンクされているコードを使用するとうまくいくので、HttpPost のエラーのようです。特定のニーズに合わせてコードを変更しましたが、これが私のコードです:(リンクがダウンした場合に備えて)

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);

//do this wherever you are wanting to POST
    URL url;
    HttpURLConnection conn;

    try{
    //if you are using https, make sure to import java.net.HttpsURLConnection
    url=new URL("https://ci.illution.dk/login/device");

    //you need to encode ONLY the values of the parameters
    String param="username=" + URLEncoder.encode("usernametest","UTF-8")+
    "&password="+URLEncoder.encode("passwordtest","UTF-8");

    conn=(HttpURLConnection)url.openConnection();
    //set the output to true, indicating you are outputting(uploading) POST data
    conn.setDoOutput(true);
    //once you set the output to true, you don't really need to set the request method to post, but I'm doing it anyway
    conn.setRequestMethod("POST");

    //Android documentation suggested that you set the length of the data you are sending to the server, BUT
    // do NOT specify this length in the header by using conn.setRequestProperty("Content-Length", length);
    //use this instead.
    conn.setFixedLengthStreamingMode(param.getBytes().length);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    //send the POST out
    PrintWriter out = new PrintWriter(conn.getOutputStream());
    out.print(param);
    out.close();

    //build the string to store the response text from the server
    String response= "";

    //start listening to the stream
    Scanner inStream = new Scanner(conn.getInputStream());

    //process the stream and store it in StringBuilder
    while(inStream.hasNextLine())
        response+=(inStream.nextLine());

        Log.d("Test", response);
    }

    //catch some error
    catch(MalformedURLException ex){
    Toast.makeText(MainActivity.this, ex.toString(), 1 ).show();

    }
    // and some more
    catch(IOException ex){

    Toast.makeText(MainActivity.this, ex.toString(), 1 ).show();
    }
于 2012-10-04T18:09:16.563 に答える