如何验证处理的 HTTP 请求的证书 class

How do I authenticate certificate for Processing's HTTP Request class

我正在使用 Rune Madson 和 Daniel Shiffman 的 HTTP-Request-for-Processing class 来处理 GetRequest 和 PostRequest 以便与站点 OAuth 一起使用。

我输入正确的URL和要处理的参数:

GetRequest req = new GetRequest("https://www.afakesite.com/oauth2/token");
req.addHeader("grant_type","authorization_code");
req.addHeader("client_id",ID);
req.addHeader("client_secret",fancyClientSecret);
req.addHeader("code",authorizationCode);
req.send();

但我遇到了这些错误:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

我也尝试过使用Processing方法:

loadJSONObject("https://www.afakesite.com/oauth2/token?grant_type=authorization_code&client_id=...")

在没有上述错误的情况下成功运行,所以我知道站点正在返回数据,但我还想使用 PostRequest class 将文件提交到站点。此命令也不会使用站点提供的附加 JSON 文件报告 400 错误,而是丢弃它并给我一个例外。


那么我应该如何在处理中验证此请求,如果不可能或太复杂,我应该如何将文件发送到此站点。

如果该信息有任何用处,我正在尝试验证的网站是 DeviantArt。

我建议您尝试在没有图书馆的情况下进行一些工作。您可以只使用标准 Java API.

中的 HttpsUrlConnection

这里有一个小例子,posts 一些数据到 URL:

import javax.net.ssl.HttpsURLConnection;
import java.io.OutputStreamWriter;
import java.net.URL;

HttpsURLConnection connection = (HttpsURLConnection) new URL("https://example.com").openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("param1=Data for param1");
writer.write("&param2=Data for param2"); //Ampersand is necessary for more than one parameter
writer.write("&param3=Data for param3");
writer.flush();

int responseCode = connection.getResponseCode();
if(responseCode == 200){
   System.out.println("POST was successful!");
}
else{
   System.out.println("Error: " + responseCode);
}

无耻的自我推销:还有一些示例(包括指定身份验证)可用 here

如果您能让它正常工作,那么您就知道库本身有问题。老实说,自己做 post 并不会占用太多代码行,因此您可以完全摆脱库。


EDIT Processing 将不会编译代码,因为 MalformedURLException 可以通过封装在 "try" 块中来避免 IE。

try {
   HttpsURLConnection connection = (HttpsURLConnection) new URL("https://example.com").openConnection();
   //so on and so forth...
} catch(Exception e) {}