需要帮助将curl请求转换为java请求吗

需要帮助将curl请求转换为java请求吗,java,http,curl,post,Java,Http,Curl,Post,我有一个特定的卷曲要求,如下所示- curl -POST -H 'access-key: <apikey>' -H "Content-type: application/json" -d '{ "item": "electricity", "region": "india", "unit": "kWh", "quantity": 1.564}' 'https://www.carbonhub.xyz/v1/emissions'` curl-POST-H'访问键:'-H'内容类型:ap

我有一个特定的卷曲要求,如下所示-

curl -POST -H 'access-key: <apikey>' -H "Content-type: application/json" -d '{
"item": "electricity",
"region": "india",
"unit": "kWh",
"quantity": 1.564}' 'https://www.carbonhub.xyz/v1/emissions'`
curl-POST-H'访问键:'-H'内容类型:application/json'-d'{
“项目”:“电力”,
“地区”:“印度”,
“单位”:“kWh”,
“数量”:1.564}”https://www.carbonhub.xyz/v1/emissions'`
我试着为同样的东西制作一个java对应物,这就是我到目前为止所想到的-

package org.kodejava.example.httpclient;    
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class getEmissions {
    public static void main(String[] args) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("https://www.carbonhub.xyz/v1/emissions");

        List<NameValuePair> data = new ArrayList<>(4); 
        data.add(new BasicNameValuePair("item", "electricity"));
        data.add(new BasicNameValuePair("region", "india"));
        data.add(new BasicNameValuePair("unit", "kWh"));
        data.add(new BasicNameValuePair("quantity", 1.564));

        try {
            post.setEntity(new UrlEncodedFormEntity(data));
            post.setHeader("Content-Type","application/json");
            // use your api key
            post.setHeader("access-key","<apikey>");
            HttpResponse response = client.execute(post);

            // Print out the response message
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package org.kodejava.example.httpclient;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.HttpClientBuilder;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.util.EntityUtils;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类排放{
公共静态void main(字符串[]args){
HttpClient client=HttpClientBuilder.create().build();
HttpPost=新的HttpPost(“https://www.carbonhub.xyz/v1/emissions");
列表数据=新的ArrayList(4);
数据。添加(新的BasicNameValuePair(“项目”、“电力”);
添加(新的BasicNameValuePair(“地区”、“印度”);
添加(新的BasicNameValuePair(“单位”、“千瓦时”);
添加(新的BasicNameValuePair(“数量”,1.564));
试一试{
post.setEntity(新的UrlEncodedFormEntity(数据));
setHeader(“内容类型”、“应用程序/json”);
//使用api密钥
post.setHeader(“访问键”,即“);
HttpResponse response=client.execute(post);
//打印出响应消息
System.out.println(EntityUtils.toString(response.getEntity());
}捕获(IOE异常){
e、 printStackTrace();
}
}
}

请让我知道如何解决这个问题。提前感谢。

您在
POST
请求中发送数据的方式与指定的
curl
命令不同

您应该首先为输入创建一个
JSON
字符串。有多种库可用于此目的,如
Jackson
JSON-java
Gson
等。或者您可以手动构造
JSON
字符串(不推荐),然后您应该在
POST
请求中将
JSON
字符串作为数据发送

下面是一种手动构造
JSON
字符串并将其作为
POST
数据发送的方法-

package org.kodejava.example.httpclient;    

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class getEmissions {
    public static void main(String[] args) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("https://www.carbonhub.xyz/v1/emissions");

        StringBuilder requestData = new StringBuilder("'{");
        requestData.append("\"item\"").append(':').append("\"electricity\"").append(',');
        requestData.append("\"region\"").append(':').append("\"india\"").append(',');
        requestData.append("\"unit\"").append(':').append("\"kWh\"").append(',');
        requestData.append("\"quantity\"").append(':').append("1.564");
        requestData.append("}'");

        StringEntity requestDataEntity = new StringEntity(requestData.toString(),ContentType.APPLICATION_JSON);
        try {
            post.setEntity(requestData);
            // use your api key
            post.setHeader("access-key","<apikey>");
            HttpResponse response = client.execute(post);

            // Print out the response message
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package org.kodejava.example.httpclient;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.HttpClientBuilder;
导入org.apache.http.util.EntityUtils;
导入java.io.IOException;
公共类排放{
公共静态void main(字符串[]args){
HttpClient client=HttpClientBuilder.create().build();
HttpPost=新的HttpPost(“https://www.carbonhub.xyz/v1/emissions");
StringBuilder requestData=新的StringBuilder(“{”);
requestData.append(“\”项\“).append(“:”).append(“\”电\“).append(“,”);
requestData.append(“\”region\”).append(“:”).append(“\”india\”).append(“,”);
requestData.append(“\”单位“).append(“:”).append(“\”千瓦时“).append(“,”);
requestData.append(“\”数量“).append(“:”).append(“1.564”);
requestData.append(“}”);
StringEntity requestDataEntity=新的StringEntity(requestData.toString(),ContentType.APPLICATION_JSON);
试一试{
post.setEntity(requestData);
//使用api密钥
post.setHeader(“访问键”,即“);
HttpResponse response=client.execute(post);
//打印出响应消息
System.out.println(EntityUtils.toString(response.getEntity());
}捕获(IOE异常){
e、 printStackTrace();
}
}
}

UrlEncodedFormEntity
用于编码内容类型
application/x-www-form-urlencoded
。您需要将
数据
转换为JSON,最好使用JSON库。