0

目前我有以下代码,一切都很好,除了我想从主机读取传入数据而不重复绘制函数。特别是因为我在编写的程序中不需要绘图功能。虽然循环似乎不起作用......

import processing.net.*;

String data;
Client c;
String hostvar;
String getReq;


void setup() {

  hostvar = "www.processing.org";  
  getReq = "/reference";

  c = new Client(this, hostvar, 80); // Connect to server on port 80

    //c.write("GET / HTTP/1.1\r\n"); // Use the HTTP "GET" command to ask for a Web page

  c.write("GET "+ getReq + " HTTP/1.1\r\n");
  c.write("Host:" + hostvar + "\r\n"); 
  c.write("User-Agent: Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.12; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7\n");
  c.write("\r\n");

  while (c.available() > 0) { // This code doesn't work. .
    data = c.readString(); // // This code doesn't work. .
    println(data); // This code doesn't work. .
  }// This code doesn't work. .
}

void draw() {

    if (c.available() > 0) { // If there's incoming data from the client...
      data = c.readString(); // ...then grab it and print it  //this code works...
      println(data);
    }
}
4

3 回答 3

1

这也应该有效(如果您不想弄乱任何罐子),尽管您可能需要调整请求标头以使其看起来像您想要的任何“真实”浏览器。

import java.io.*;
import java.net.*;

void setup() {
  String s = getHTML("http://www.processing.org/reference");
  println(s);
}

String getHTML(String url) {

  String line, result = "";

  try {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestMethod("GET");
    setRequestHeaders(conn);
    BufferedReader rd = new BufferedReader
      (new InputStreamReader(conn.getInputStream()));
    while ( (line = rd.readLine ()) != null) {
      result += line;
    }
    rd.close();
  } 
  catch (Exception e) {
    e.printStackTrace();
  }

  return result;
}

void setRequestHeaders(HttpURLConnection conn)
{
  String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
  conn.setRequestProperty("User-Agent", ua);
  conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
  conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
  conn.setRequestProperty("Connection", "keep-alive");
  conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
}
于 2013-10-05T08:44:18.437 回答
1

最好的方法是可能使用 Apache HttpComponents-Client。从此处下载最新的“二进制”zip 文件 . 打开它并找到“lib”文件夹中的所有 jar 文件。然后将它们直接拖到处理 IDE 中,如此所述。然后您应该能够使用“示例”文件夹中的任何代码。这是一个简单的:

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

void setup() {

  CloseableHttpClient httpclient = HttpClients.createDefault();
  HttpGet httpGet = new HttpGet("http://targethost/homepage");
  CloseableHttpResponse response1 = httpclient.execute(httpGet);

  try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();

    // do something with the response body,
    // then ensure it is fully consumed

    EntityUtils.consume(entity1);
  } 
  finally {
    response1.close();
  }

}
于 2013-10-05T08:30:43.163 回答
1

如果您只想获取数据:

void setup() {

  String hostvar = "http://www.processing.org/reference";
  println(join(loadStrings(hostvar),"\n"));
}

或者,如果您不想阻止/等待结果:

void setup() {

  new Thread() {
    public void run() {
      String hostvar = "http://www.processing.org/reference";
      println(PApplet.join(loadStrings(hostvar),"\n"));
    }
  }.start();

}
于 2013-09-25T14:34:16.273 回答