【问题标题】:Send form by post method in Java在 Java 中通过 post 方法发送表单
【发布时间】:2015-08-04 11:04:37
【问题描述】:

我需要通过 Java 或 Groovy 代码发送此表单:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
    <input type="hidden" name="business" value="xxx@xxx.com">
    <input type="hidden" name="currency_code" value="EUR">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="item_name_1" value="zzzzz zzzz zzz">
    <input type="hidden" name="item_number_1" value="11111111">
    <input type="hidden" name="item_quantity_1" value="2">
    <input type="hidden" name="amount_1" value="11.11">

    <input type="image" src="xxxxxxx" border="0" name="submit">
    <img border="0" src="xxxxxxxxxxxxx" width="1" height="1">
</form>

我需要代码来发送发送此表单的相同请求。

这不是一个正常的发帖请求。当您提交此 HTML 时,您将被重定向到 PayPal。我需要从 Java 重定向到 PayPal,但通过 post 方法发送数据。

【问题讨论】:

  • 有什么问题?
  • 使用 apache Httpclient
  • 用 java 生成和发送相同的 http POST 请求应该很容易。我不明白“我需要重定向到贝宝”部分。发生重定向是因为服务器告诉您访问特定的 URL。检查服务器对该 URL 的响应。

标签: grails paypal


【解决方案1】:

试试这个代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;  
import java.net.MalformedURLException;
import java.net.URL;


public class Sender {

public static void main(String[] args) {


    sendPost("xxx@xxx.com", "EUR", "_cart", "1", "zzzzz zzzz zzz", "11111111", "2", "11.11");
}

public static String sendPost(String business,String currency_code,String cmd,String upload,String item_name_1,String item_number_1,
        String item_quantity_1,String amount_1
        )
{
    try {
        URL url = new URL("https://www.sandbox.paypal.com/cgi-bin/webscr");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestProperty("Content-Type", "text/html");
        conn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
        String request="business="+business+"&currency_code="+currency_code+
                "&cmd="+cmd+"&upload="+upload+"&item_name_1="+item_name_1+"&item_number_1="+item_number_1+
                "&item_quantity_1="+item_quantity_1+"&amount_1="+amount_1;
        writer.write(request);
        writer.flush();
        System.out.println("Code:"+conn.getResponseCode());
        System.out.println("mess:"+conn.getResponseMessage());


        String response="";
        BufferedReader reader = new BufferedReader(new  InputStreamReader(conn.getInputStream(),"UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            response+=line;
        }

        System.out.println(new String(response.getBytes(),"UTF8"));
        writer.close();
        reader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 2014-07-08
    • 1970-01-01
    • 2011-05-11
    • 2018-06-13
    • 2022-08-16
    相关资源
    最近更新 更多