我的网址:

curl URL -X POST -d'{“经度”:“ 5.55”,“纬度”:“ 6.66”,“地点”:“ hello world”,“描述”:“此地点可通过轮椅进入”}'-u user:password -H'内容类型: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错误。
请帮助=。=

最佳答案

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

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

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

09-10 05:06
查看更多