java - PayPal 的未知主机异常

标签 java api paypal unknown-host

我正在尝试使用 PayPal 按钮管理器 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: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 

如有任何帮助,我们将不胜感激。我看到大多数人建议尝试通过浏览器访问 URL,这在我的例子中是可行的,只是在使用我的 java 类时不行。

提前致谢! :D

最佳答案

sandbox.paypal.com/cgi-bin/webscr 不是主机名。您的主机名应该只是 sandbox.paypal.com。另外,您的路径应该是 /cgi-bin/webscr,而不是 /

另见:

关于java - PayPal 的未知主机异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11617521/

相关文章:

用于管理 vimeo * 和 * youtube 视频播放列表的 Javascript 播放器?

python - 有什么方法可以更改寄存器的 Django-rest-auth View 吗?

forms - PayPal 未预填充所有表单字段

android - Paypal 基本访问身份验证中的 URL 无效

java - 如何将多维数组传递给构造函数?

java - 复合用户类型的聚合函数

java - Android JSON 库的性能和可用性比较

java - 关于如何修复我的代码有什么建议吗?

c++ - 对 C++ Python 扩展的困惑。诸如获取 python 值的 C++ 值之类的事情

paypal - 一个应用程序与多个 Paypal 账户交互