发布HTTPUrlConnection

时间:2015-06-14 16:39:47

标签: java

我正在使用HttpUrlConnection来连接我有表单和按钮的Web服务。

源代码中的部分是:

    <form action="main.php4?page=markt3art=UIN=484504321483db0fc35d1e8e660b99c8&x=&y=filled=1" method="POST">
        <input type="hidden" value="" name="p"></input>
        <input type="hidden" value="1755219" name="buy2f8d0"></input>
        <input type="hidden" value="1755219" name="buy"></input>
        <input type="text" value="323" size="9" maxlength="16" name="teilkauf"></input>
        <img width="1" hspace="20" height="1" src="pics/leer.gif"></img>
        <input class="send" type="submit" value=" buy "></input>
    </form>

我正在做的是:(www是网站http:// ...)

            URL url = new URL(www);
            HttpURLConnection conexion = (HttpURLConnection)url.openConnection();
            String forSending = "1";
            String charset = "UTF-8";

            String stringToSend = URLEncoder.encode(forSending, charset);

            conexion.setDoOutput(true);
            conexion.setRequestMethod("POST");
            OutputStreamWriter out = new OutputStreamWriter(
            conexion.getOutputStream());

            out.write("teilkauf=" + stringToSend);
            out.flush();
            out.close();

但它不起作用。有帮助吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您必须对表单的操作发出POST请求,而不是表单所在的站点。

当您单击提交浏览器构建对操作的请求时,表单就像后端服务器的前端。

因此,而不是POST到表单的位置POST到main.php4?page = markt3art = UIN = 484504321483db0fc35d1e8e660b99c8&amp; x =&amp; y = filled = 1