简体   繁体   English

使用HTTPUrlConnection发布

[英]Post with HTTPUrlConnection

I'm using HttpUrlConnection to connect a web service where I have a form and a button. 我正在使用HttpUrlConnection来连接具有表单和按钮的Web服务。

The part in the source code is: 源代码中的部分是:

    <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>

What I'm doing is: (www is the website http:// ...) 我正在做的是:(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();

But it doesn't work. 但这是行不通的。 Any help? 有什么帮助吗? Thank you. 谢谢。

You have to make a POST request to the form's action, not the site where form is located. 您必须对表单的操作(而不是表单所在的站点)发出POST请求。

Form is like a front-end for the backend server, when you click submit browser builds a request to the action. 表单就像后端服务器的前端一样,当您单击“提交”时,浏览器会为该操作生成一个请求。

So instead of POSTing to the form's location POST to main.php4?page=markt3art=UIN=484504321483db0fc35d1e8e660b99c8&x=&y=filled=1 因此,而不是POST到表单的位置POST到main.php4?page = markt3art = UIN = 484504321483db0fc35d1e8e660b99c8&x =&y = filled = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM