3

我的卷曲:

curl URL -X POST -d '{"longitude":"5.55", "latitude":"6.66", "place":"hello world", "description":"这个地方可以坐轮椅"}' -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

我找到了解决方案,只需将网址更改为:

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

但我担心的是,这安全吗?

于 2013-06-14T02:01:19.227 回答