将cURL帖子转换为HTTPclient和HttpPost。得到Http 403错误。 [JAVA]

时间:2013-06-13 08:34:04

标签: java curl http-post

我的cURL:

curl URL -X POST -d'{“经度”:“5.55”,“纬度”:“6.66”,“地方”:“你好世界”,“描述”:“这个地方可以通过轮椅进入”} '-u user:password -H'Content-type:application / 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错误。 请帮助=。=

1 个答案:

答案 0 :(得分:0)

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

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

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