我如何处理这个curl命令的处理呢?

时间:2015-09-03 17:30:33

标签: curl processing

例如,我已经获得了这个curl命令(这是一个更新列表的示例Pushbullet API请求)。卷曲参数" - 标题"用于设置HTTP标头," -X"用于HTTP请求。

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

  

curl --header' Access-Token:' -X POST ICantPostMoreThanTwoLinks --header' Content-Type:application / json' --data-binary' {" items":[{" checked":true," text":" one"} ,{"已检查":是,"文字":"两个"}]}'

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

1 个答案:

答案 0 :(得分:0)

问题由koogs回答here,非常感谢他/她的回答。

像往常一样安装库(我们需要里面的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。