Java PayPal出现未知主机异常
我正在尝试使用贝宝按钮管理器API。每次运行文件时,无论调用类型如何,都会得到以下错误输出:Java PayPal出现未知主机异常,java,api,paypal,unknown-host,Java,Api,Paypal,Unknown Host,我正在尝试使用贝宝按钮管理器API。每次运行文件时,无论调用类型如何,都会得到以下错误输出: run: Request sent to PayPal: USER=xxxxxxxxxxxxxxx&PWD=xxxxxxxxx&SIGNATURE=xxxxxxxxxxxx&METHOD=BMButtonSearch&VERSION=59.0&STARTDATE=2012-10-11T00:00:00Z&ENDDATE=2012-10-11T00:00:0
run:
Request sent to PayPal: USER=xxxxxxxxxxxxxxx&PWD=xxxxxxxxx&SIGNATURE=xxxxxxxxxxxx&METHOD=BMButtonSearch&VERSION=59.0&STARTDATE=2012-10-11T00:00:00Z&ENDDATE=2012-10-11T00:00:00Zjava.net.UnknownHostException: sandbox.paypal.com/cgi-bin/webscr
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:866)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1258)
at java.net.InetAddress.getAllByName0(InetAddress.java:1211)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at java.net.InetAddress.getAllByName(InetAddress.java:1063)
at java.net.InetAddress.getByName(InetAddress.java:1013)
at BM_ButtonSearch.main(BM_ButtonSearch.java:48)
BUILD SUCCESSFUL (total time: 0 seconds)
我的班级代码如下:
import java.net.*;
import javax.net.ssl.*;
import java.io.*;
import java.security.MessageDigest;
import java.util.Date;
public class BM_ButtonSearch {
public static void main(String[] args) {
try {
//String hostname = "paypal.com/cgi-bin/webscr"; //Set as host if live
String hostname = "sandbox.paypal.com/cgi-bin/webscr"; //Set as host if testing
// Set variables for PayPal transaction
String user = "xxxxxxxxxxxxxxxxxxxx"; //Set PayPal API username
String password = "xxxxxxxxxxx"; //API password, not acct password
String signature = "xxxxxxxxxxxxxxxxxxx"; //Set unique user signature
String method = "BMButtonSearch"; //Set Button Manager button type
String version = "59.0"; //Set Button Manager Version as ##.#
String startdate = "2012-10-11T00:00:00Z"; //Set start date using YYYY-MM-DDTHH:MM:SSZ
String enddate = "2012-10-11T00:00:00Z"; //Set end date using YYYY-MM-DDTHH:MM:SSZ
String requestID = "";
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] md5summe = md5.digest(new Date().toString().getBytes());
for (int it = 0; it < md5summe.length; it++) {
String temp = Integer.toHexString(md5summe[it] & 0xFF);
/*
* toHexString has the side effect of making stuff like 0x0F
* only one char F(when it should be '0F') so I check the length
* of string
*/
if (temp.length() < 2) {
temp = "0" + temp;
}
requestID += temp.toUpperCase();
}
// Build NVP String (currency string added, declare in variables)
String paramNVP = "USER=" + user + "&PWD=" + password + "&SIGNATURE="
+ signature + "&METHOD=" + method + "&VERSION=" + version
+ "&STARTDATE=" + startdate + "&ENDDATE=" + enddate;
System.out.print("Request sent to PayPal: " + paramNVP);
// Create the SSL Socket
int port = 443;
InetAddress addr = InetAddress.getByName(hostname);
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsock = (SSLSocket) factory.createSocket(addr, port);
// Open Socket Output Stream
String path = "/";
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sslsock.getOutputStream(), "UTF8"));
// Send data as a POST
bw.write("POST " + path + " HTTP/1.0\r\n");
// Write header info to the socket
bw.write("Connection: close\r\n");
bw.write("Content-Type: text/namevalue\r\n");
bw.write("Content-Length: " + paramNVP.length() + "\r\n");
bw.write("Host: " + hostname + "\r\n");
bw.write("X-VPS-REQUEST-ID: " + requestID + "\r\n");
bw.write("X-VPS-CLIENT-TIMEOUT: 240\r\n");
// Sets the Payflow client IP variable
// bw.write("PP_REMOTE_ADDR: 192.168.0.123\r\n");
bw.write("\r\n");
// Write the NVP String to the Socket
bw.write(paramNVP);
// Flush the stream
bw.flush();
// Read the Socket response
BufferedReader br = new BufferedReader(new InputStreamReader(
sslsock.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
// Close the writer, reader, and the socket
bw.close();
br.close();
sslsock.close();
}
catch (Exception e) {
e.printStackTrace(System.out);
}
} // end main()
}// end class SocketTest
import java.net.*;
导入javax.net.ssl.*;
导入java.io.*;
导入java.security.MessageDigest;
导入java.util.Date;
公共类BM_按钮搜索{
公共静态void main(字符串[]args){
试一试{
//字符串hostname=“paypal.com/cgi-bin/webscr”;//如果处于活动状态,则设置为主机
字符串hostname=“sandbox.paypal.com/cgi-bin/webscr”;//测试时设置为主机
//设置贝宝交易的变量
String user=“xxxxxxxxxxxxxxxxx”;//设置贝宝API用户名
字符串password=“xxxxxxxxxx”//API密码,而不是帐户密码
字符串签名=“xxxxxxxxxxxxxxxx”;//设置唯一的用户签名
String method=“BMButtonSearch”;//设置按钮管理器按钮类型
String version=“59.0”//将按钮管理器版本设置为###
String startdate=“2012-10-11T00:00:00Z”;//使用YYYY-MM-DDTHH:MM:SSZ设置开始日期
字符串enddate=“2012-10-11T00:00:00Z”;//使用YYYY-MM-DDTHH:MM:SSZ设置结束日期
字符串requestID=“”;
MessageDigest md5=MessageDigest.getInstance(“md5”);
byte[]md5summe=md5.digest(new Date().toString().getBytes());
for(int it=0;it
任何帮助都将不胜感激。我发现大多数人都建议尝试通过浏览器访问URL,在我的例子中,这是可行的,但在使用java类时就不行了
提前谢谢!:D
sandbox.paypal.com/cgi-bin/webscr
不是主机名。您的主机名应该是sandbox.paypal.com。此外,您的路径应该是/cgi-bin/webscr
,而不是/
另见:
Set Cookie
请求客户端(在本例中是您的Java代码),为将来的请求设置指定的Cookie。这在HTTP这样的无状态协议中非常常见,它将第一个请求与将来要关联的请求绑定在一起。通常情况下,这些都会被浏览器或其他高级HTTP浏览技术隐藏起来。为什么不使用更高级的Java Web客户端类来自动处理这些低级HTTP详细信息?您不应该使用Java之类的语言来处理HTTP头。