3

私のcURL:

curl URL -X POST -d '{"経度":"5.55", "緯度":"6.66", "場所":"hello world", "説明":"この場所は車椅子でアクセスできます"}' -uユーザー: パスワード -H 'コンテンツ タイプ: アプリケーション/json'

私のJavaコード:

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("user", "password"));

    HttpPost httppost = new HttpPost("URL");

    httppost.setHeader("Content-type", "application/json");

    JSONObject json = new JSONObject();
    try {

        json.put("longitude", "1");
        json.put("latitude", "2");
        json.put("place", "3");
        json.put("description", "4");
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }

    try {

        StringEntity entity = new StringEntity(json.toString(), HTTP.UTF_8);
        // entity.setContentType("application/json");

        httppost.setEntity(entity);

        HttpResponse resp = httpclient.execute(httppost);

        System.out.println(resp.getStatusLine().getStatusCode() + "/"
                + resp.getStatusLine().getReasonPhrase());
    } catch (Exception e) {
        System.err.println(e.getMessage());

    }

    System.out.println("SUCCESS: " + result);

cURL は正しく実行されますが、JAVA コードから 403 エラーが返されます。助けてください =.=

4

1 に答える 1

0

解決策を見つけました。URLを次のように変更するだけです:

https://username:password@www.example.com/path

しかし、私の懸念は、これは安全ですか?

于 2013-06-14T02:01:19.227 に答える