我怎样才能对处理这个 curl 命令做同样的事情?

How could I do the same with Processing that this curl command does?

例如,我有这个 curl 命令(这是一个更新列表的 Pushbullet API 请求示例)。 curl参数“--header”用于设置HTTP头,“-X”用于HTTP请求。

我无法通过谷歌搜索找到解决方案。

curl --header 'Access-Token: ' -X POST ICantPostMoreThanTwoLinks --header 'Content-Type: application/json' --data-binary '{"items": [{"checked": true, "text": "one"}, {"checked": true, "text": "two"}]}'

因为我想在 Processing 中使用 Pushbullet API 在我的设备上显示 Pushbullet 消息,所以我如何才能在 Processing 代码中执行 curl 命令的相同操作?

问题已由 koogs here 回答,非常感谢 him/her 的回答。

照常安装库(我们需要里面的 lib jar)。

必须将以下 java 代码添加到项目中另一个名为 "PostRequest.java" 的选项卡中。

// 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"));
}

请注意 JSON 必须转义。