简体   繁体   English

通过Java中的Apache http客户端连接到Prestashop

[英]Connect to Prestashop through Apache http client in java

I am trying upload images to prestashop through httpclient of Apache but when I execute my script, I have a error of HTTP/1.1 401 Unauthorized. 我正在尝试通过Apache的httpclient将图像上传到prestashop,但是当我执行脚本时,出现HTTP / 1.1 401 Unauthorized错误。 I hope that you can help me. 我希望你能帮助我。 Thank you very much. 非常感谢你。

private void addImg(String imgURL, int productId) throws Exception {


    URL imgUrl = new URL(imgURL);
    InputStream is = imgUrl.openStream();

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
    buffer.flush();

    String completeUrlString = URLSTRING + "/api/images/products/" + String.valueOf(productId);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope("localhost/prestashop", 80),
            new UsernamePasswordCredentials("XV4J9MNC1WPMCDMAAXJ1MRMCEAT9DJDJ", ""));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

    HttpPost httppost = new HttpPost(completeUrlString);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart("image", new ByteArrayBody(buffer.toByteArray(), "upload.jpg"));

    HttpEntity entity = builder.build();
    httppost.setEntity(entity);

    CloseableHttpResponse response = httpclient.execute(httppost);
    System.out.println(response.getStatusLine());
}

Resolved: 解决:

private void addImg(String imgURL, int productId) throws Exception {


    URL imgUrl = new URL(imgURL);
    InputStream is = imgUrl.openStream();

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
    buffer.flush();

    String completeUrlString = URLSTRING + "/api/images/products/" + String.valueOf(productId);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            AuthScope.ANY,
            new UsernamePasswordCredentials("XV4J9MNC1WPMCDMAAXJ1MRMCEAT9DJDJ", ""));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

    HttpPost httppost = new HttpPost(completeUrlString);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart("image", new ByteArrayBody(buffer.toByteArray(), "img.jpg"));

    HttpEntity entity = builder.build();
    httppost.setEntity(entity);

    HttpResponse response = httpclient.execute(httppost);
    System.out.println(response.getStatusLine());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM