|
Rank: Starting Member
Groups: Registered
Joined: 5/8/2009 Posts: 2 Location: ,
|
When I try to use Java HttpClient and the PostMethod classes to perform the form submit that takes a user to the PayPal Sandbox it always ends up at the PayPal Sandbox screen that says: "Please login to use the PayPal Sandbox features." ... even though I have already logged on to my sandbox account.
If I use the PayPal generated form (with the same parameters) it does take me to the correct sandbox test user login screen. However, I don't want to use the PayPal generated form. I want to generate and submit the form within some Java code.
I noticed that a number of people are doing that with PHP but I haven't seen a Java example. Does anyone have an example of doing this?
|
|
Rank: Starting Member
Groups: Registered
Joined: 5/8/2009 Posts: 2 Location: ,
|
Here is the section of my Java code where I create the post to PayPal -
log.info("<BuyCreditsConfirmAction> - onConfirm method...");
String url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("https://www.sandbox.paypal.com/cgi-bin/webscr"); postMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
postMethod.addParameter("cmd", "_s-xclick"); postMethod.addParameter("hosted_button_id", "31991"); postMethod.addParameter("on0", "$20.00"); postMethod.setRequestHeader("Content-type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE);
String responseBody = null; try {
log.info("<BuyCreditsConfirmAction> - onConfirm - calling HttpClient post method...."); int statusCode = client.executeMethod(postMethod); log.info("<BuyCreditsConfirmAction> - onConfirm - status code: " + statusCode); if (statusCode != HttpStatus.SC_OK) { log.info("<BuyCreditsConfirmAction> onConfirm - HTTPS Post failed [" + url + ", " + postMethod.getStatusText() + "]"); throw new IOException("HTTPS Post failed [" + url + ", " + postMethod.getStatusText() + "]"); }
InputStream is = postMethod.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(is); StringBuffer sb = new StringBuffer(); byte[] bytes = new byte[8192]; // reading as chunk of 8192 bytes int count = bis.read(bytes); while( count != -1 && count <= 8192) { String datastr = new String(bytes, 0, count); sb.append(datastr); count = bis.read(bytes); }
bis.close(); responseBody = sb.toString(); log.info("<BuyCreditsConfirmAction> onConfirm - Recieved response from server...\n" + responseBody);
} finally { log.info("<BuyCreditsConfirmAction> onConfirm - release connection..."); postMethod.releaseConnection(); }
response.setContentType("text/html; charset=UTF-8"); PrintWriter writer = response.getWriter(); writer.print(responseBody);
return null;
|