我如何处理这个curl命令所做的相同操作?
例如,我得到了这个curl命令(这是一个更新列表的示例请求)。Curl参数“-header”用于设置HTTP头,“-X”用于HTTP请求 我无法通过谷歌搜索找到解决方案 curl--header'Access Token:'-X POST ICANTPostHostThorTwolinks--header'Content Type:application/json'--data binary'{“items”:[{“checked”:true,“text”:“one”},{“checked”:true,“text”:“two”} 因为我想在处理过程中使用Pushbullet API在我的设备上显示Pushbullet消息,我怎么能在代码中使用curl命令呢?koogs回答了这个问题,非常感谢他/她给出的答案 像往常一样安装库(我们需要库中的lib jar) 必须将以下java代码添加到项目中名为“PostRequest.java”的另一个选项卡中 注意,必须转义JSON 交叉张贴:我如何处理这个curl命令所做的相同操作?,curl,processing,Curl,Processing,例如,我得到了这个curl命令(这是一个更新列表的示例请求)。Curl参数“-header”用于设置HTTP头,“-X”用于HTTP请求 我无法通过谷歌搜索找到解决方案 curl--header'Access Token:'-X POST ICANTPostHostThorTwolinks--header'Content Type:application/json'--data binary'{“items”:[{“checked”:true,“text”:“one”},{“checked”:t
// mostly from the httprequests-for-processing library
// header support added - acd 2015-09-05
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class PostRequest {
String url;
ArrayList<BasicNameValuePair> nameValuePairs;
HashMap<String, File> nameFilePairs;
List<Header> headers;
String content;
String encoding;
HttpResponse response;
String json;
public PostRequest(String url) {
this(url, "ISO-8859-1");
}
public PostRequest(String url, String encoding) {
this.url = url;
this.encoding = encoding;
nameValuePairs = new ArrayList<BasicNameValuePair>();
nameFilePairs = new HashMap<String, File>();
headers = new ArrayList<Header>();
}
public void addData(String key, String value) {
BasicNameValuePair nvp = new BasicNameValuePair(key, value);
nameValuePairs.add(nvp);
}
public void addJson(String json) {
this.json = json;
}
public void addFile(String name, File f) {
nameFilePairs.put(name, f);
}
public void addFile(String name, String path) {
File f = new File(path);
nameFilePairs.put(name, f);
}
public void addHeader(String name, String value) {
headers.add(new BasicHeader(name, value));
}
public void send() {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
if (nameFilePairs.isEmpty()) {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
} else {
MultipartEntity mentity = new MultipartEntity();
Iterator<Entry<String, File>> it = nameFilePairs.entrySet().iterator();
while (it.hasNext ()) {
Entry<String, File> pair = it.next();
String name = (String) pair.getKey();
File f = (File) pair.getValue();
mentity.addPart(name, new FileBody(f));
}
for (NameValuePair nvp : nameValuePairs) {
mentity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
}
httpPost.setEntity(mentity);
}
// add the headers to the request
if (!headers.isEmpty()) {
for (Header header : headers) {
httpPost.addHeader(header);
}
}
// add json
if (json != null) {
StringEntity params =new StringEntity(json);
httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
httpPost.setEntity(params);
}
response = httpClient.execute( httpPost );
HttpEntity entity = response.getEntity();
this.content = EntityUtils.toString(response.getEntity());
if ( entity != null ) EntityUtils.consume(entity);
httpClient.getConnectionManager().shutdown();
// Clear it out for the next time
nameValuePairs.clear();
nameFilePairs.clear();
} catch( Exception e ) {
e.printStackTrace();
}
}
/*
** Getters
*/
public String getContent() {
return this.content;
}
public String getHeader(String name) {
Header header = response.getFirstHeader(name);
if (header == null) {
return "";
} else {
return header.getValue();
}
}
}
import http.requests.*;
void setup() {
PostRequest post = new PostRequest(URL);
post.addHeader("Content-Type", "application/json");
post.addJson("{\"items\": [{\"checked\": true, \"text\": \"one\"}, {\"checked\": true, \"text\": \"two\"}]}");
post.send();
System.out.println("Reponse Content:" + post.getContent() + "\n");
System.out.println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
}