简体   繁体   English

将curl请求转换为Java请求

[英]Convert curl request to java request

I need to create http request to google web store for upload items. 我需要向Google网上商店创建http请求以上传项目。

I did it in curl and it worked. 我做到了,它奏效了。

curl -H "Authorization: Bearer Mytoken"  -H "x-goog-api-version: 2" -X POST -T /Users/mac/Downloads/file_name_3.25.10.zip -v https://www.googleapis.com/upload/chromewebstore/v1.1/items

I want to know how to pass -T file value. 我想知道如何传递-T文件值。 This is my code. 这是我的代码。

URL url = new URL("https://www.googleapis.com/upload/chromewebstore/v1.1/items");
Map<String, String> params  = new HashMap<String, String>();
params.put("FILE",filePath);
Map<String, String> requestPropertys  = new HashMap<String, String>();
requestPropertys.put("Authorization","Bearer " + 
GoogleWebStoreDeveloperAuthService.getAccess_token());
requestPropertys.put("x-goog-api-version","2");
requestPropertys.put("Accept","*/*");
content = NetworkUtil.crossApplicationRequestPost(url, params, requestPropertys);

public static String crossApplicationRequestPost(URL url, Map params, Map requestPropertys ) { 公共静态字符串crossApplicationRequestPost(URL网址,地图参数,地图requestPropertys){

    String result = null;

    String param = getQueryString(params);
    param = addHashQuery(param);

    /* End preparing params */
    byte[] postData = param.getBytes(Charset.forName("UTF-8"));
    int postDataLength = postData.length;

    log.info(" url :"+url);

    try {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        for (Entry<String, T> entry : requestPropertys.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            connection.setRequestProperty(key, value.toString()); 

        }
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("charset", "utf-8"); 
        connection.setRequestProperty("Content-Length",Integer.toString(postDataLength));
        connection.setUseCaches(false);



        try (DataOutputStream wr = new DataOutputStream(
                connection.getOutputStream())) {
            wr.write(postData);
        } 

        log.info(" crossApplicationRequestPost :"+connection.getResponseCode());

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            result = IOUtils.toString(inputStream, "UTF-8");
            inputStream.close();
        } else {
            log.severe("try fails. Connect to:" + url.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

In this StackOverflow question is the solution of the problem on how to upload a file in a multipart POST HTTP request. 这个StackOverflow问题中,是如何在多部分POST HTTP请求中上载文件的问题的解决方案。

Aside from that, I really recommend you to use some type of helper to help you to make your requests easier. 除此之外,我真的建议您使用某种类型的帮助程序来帮助您简化请求。 There are plenty of them, but a popular one (and with a lot of documentation) is Square's OKHTTP library 它们很多,但是Square的OKHTTP库是一个受欢迎的(并且有很多文档)

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

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