【问题标题】:Convert cURL post into HTTPclient and HttpPost. Getting Http 403 Error. [JAVA]将 cURL post 转换为 HTTPclient 和 HttpPost。获取 Http 403 错误。 [JAVA]
【发布时间】:2013-06-09 13:51:22
【问题描述】:

我的卷曲:

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 错误。 请帮忙=.=

【问题讨论】:

    标签: java curl http-post


    【解决方案1】:

    我找到了解决办法,把url改成:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-21
      • 2017-04-27
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多