Servlets 为什么JavaServlet可以';每次都不能收到Paypal IPN消息?
我有一个Java servlet在我的笔记本电脑上运行Windows Vista,我设置了一个静态IP,做了端口转发并注册了一个免费的DDNS服务,现在我的servlet正在运行,我把url给Paypal给我发送IPN消息,我继续到它的沙箱站点,进入测试工具页面,试图通过单击“发送IPN”按钮发送测试消息,大多数情况下都会失败,错误是:“IPN传递失败。无法连接到指定的URL。请验证URL并重试。” 但可能十分之一的情况下,它可能会成功,我的servlet会得到消息,我查看了我得到的消息,它们的格式是正确的。所以我打电话给Paypal问为什么,他说我不应该在我的笔记本上运行servlet,而应该在web服务器上运行它,但我告诉他我的ISP不支持他们的服务器上的Java,既然我完成了上述所有步骤,在我的笔记本上运行servlet不应该是一样的吗?他说他的测试显示他无法访问我的servlet,但我问为什么10次中就有1次可以通过?如果在我的笔记本上运行它有什么问题,那么它应该100%失败,这一点我正确吗?但不管怎样,他说这是他所能做的,我应该自己解决它。servlet如下所示:Servlets 为什么JavaServlet可以';每次都不能收到Paypal IPN消息?,servlets,paypal,message,paypal-ipn,Servlets,Paypal,Message,Paypal Ipn,我有一个Java servlet在我的笔记本电脑上运行Windows Vista,我设置了一个静态IP,做了端口转发并注册了一个免费的DDNS服务,现在我的servlet正在运行,我把url给Paypal给我发送IPN消息,我继续到它的沙箱站点,进入测试工具页面,试图通过单击“发送IPN”按钮发送测试消息,大多数情况下都会失败,错误是:“IPN传递失败。无法连接到指定的URL。请验证URL并重试。” 但可能十分之一的情况下,它可能会成功,我的servlet会得到消息,我查看了我得到的消息,它们的
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class PayPal_Servlet extends HttpServlet
{
static boolean Debug=true;
static String PayPal_Url="https://www.paypal.com/cgi-bin/webscr",Sandbox_Url="https://www.sandbox.paypal.com/cgi-bin/webscr",
Dir_License_Messages="C:/Dir_License_Messages/";
static TransparencyExample Transparency_Example;
static PayPal_Message_To_License_File_Worker PayPal_message_to_license_file_worker;
// Initializes the servlet.
public void init(ServletConfig config) throws ServletException
{
super.init(config);
if (!new File(Dir_License_Messages).exists()) new File(Dir_License_Messages).mkdirs();
System.gc();
}
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
// Read post from PayPal system and add 'cmd'
Enumeration en=request.getParameterNames();
String str="cmd=_notify-validate";
while (en.hasMoreElements())
{
String paramName=(String)en.nextElement();
String paramValue=request.getParameter(paramName);
str=str+"&"+paramName+"="+URLEncoder.encode(paramValue);
}
// Post back to PayPal system to validate
// NOTE: change http: to https: in the following URL to verify using SSL (for increased security).
// using HTTPS requires either Java 1.4 or greater, or Java Secure Socket Extension (JSSE) and configured for older versions.
URL u=new URL(Debug?Sandbox_Url:PayPal_Url);
URLConnection uc=u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
PrintWriter pw=new PrintWriter(uc.getOutputStream());
pw.println(str);
pw.close();
BufferedReader in=new BufferedReader(new InputStreamReader(uc.getInputStream()));
String res=in.readLine();
in.close();
// Assign posted variables to local variables
String itemName=request.getParameter("item_name");
String itemNumber=request.getParameter("item_number");
String paymentStatus=request.getParameter("payment_status");
String paymentAmount=request.getParameter("mc_gross");
String paymentCurrency=request.getParameter("mc_currency");
String txnId=request.getParameter("txn_id");
String receiverEmail=request.getParameter("receiver_email");
String payerEmail=request.getParameter("payer_email");
if (res.equals("VERIFIED")) // Check notification validation
{
// check that paymentStatus=Completed
// check that txnId has not been previously processed
// check that receiverEmail is your Primary PayPal email
// check that paymentAmount/paymentCurrency are correct
// process payment
}
else if (res.equals("INVALID")) // Log for investigation
{
}
else // Log for error
{
}
// ===========================================================================
if (txnId!=null)
{
Write_File_Safe_Fast(Dir_License_Messages+txnId+".txt",new StringBuffer(str.replace("&","\n")),false);
}
// ===========================================================================
String Message_File_List[]=Tool_Lib.Get_File_List_From_Dir(Dir_License_Messages);
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String title="Reading All Request Parameters",Name="",Value;
out.println("<Html><Head><Title>"+title+"</Title></Head>\n<Body Bgcolor=\"#FDF5E6\">\n<H1 Align=Center>"+title+"</H1>\n"+
"<Table Border=1 Align=Center>\n"+"<Tr Bgcolor=\"#FFAD00\"><Th>Parameter Name</Th><Th>Parameter Value(s) Messages = "+Message_File_List.length+"</Th></Tr>");
Enumeration paramNames=request.getParameterNames();
while(paramNames.hasMoreElements())
{
String paramName=(String)paramNames.nextElement();
out.print("<Tr><Td>"+paramName+"</Td><Td>");
String[] paramValues=request.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue=paramValues[0];
if (paramValue.length() == 0) out.print("<I>No Value</I>");
else
{
out.println(paramValue+"</Td></Tr>");
// Out("paramName = "+paramName+" paramValue = "+paramValue);
// if (paramName.startsWith("Name")) Name=paramValue;
// else if (paramName.startsWith("Value")) Write_File_Safe_Fast("C:/Dir_Data/"+Name,new StringBuffer(paramValue),false);
}
}
else
{
out.println("<Ul>");
for (int i=0;i<paramValues.length;i++) out.println("<Li>"+paramValues[i]);
out.println("</Ul></Td</Tr>");
}
}
out.println("</Table>\n</Body></Html>");
}
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { processRequest(request,response); }
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { processRequest(request,response); }
// Returns a short description of the servlet.
public String getServletInfo() { return "Short description"; }
// Destroys the servlet.
public void destroy() { System.gc(); }
public static void Write_File_Safe_Fast(String File_Path,StringBuffer Str_Buf,boolean Append)
{
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try
{
fos=new FileOutputStream(File_Path,Append);
bos=new BufferedOutputStream(fos);
for (int j=0;j<Str_Buf.length();j++) bos.write(Str_Buf.charAt(j));
}
catch (Exception e) { e.printStackTrace(); }
finally
{
try
{
if (bos!=null)
{
bos.close();
bos=null;
}
if (fos!=null)
{
fos.close();
fos=null;
}
}
catch (Exception ex) { ex.printStackTrace(); }
}
System.gc();
}
}
我使用Netbean6.7开发servlet,代码来自Paypal的JSP示例代码,我可以做些什么来调试问题?嗨,请尝试使用我的库:
我希望它能帮助你。如果您有任何问题,请与我联系。您可以在源代码的注释中找到我的电子邮件。我不确定问题的根本原因,但为了避免您的(网络)环境受到怀疑,我建议在免费JSP/Servlet主机上试用它。嗯,实际上没有免费的,但至少你可以得到半小时的免费试用部署。您只需在半小时后关机时手动重新启动。另一个(不是免费的,但相当便宜)选择是谷歌Appengine很棒,谢谢你的建议!