代码之家  ›  专栏  ›  技术社区  ›  Jon Willis

我的HttpURLConnection表单似乎无法使用POST

  •  1
  • Jon Willis  · 技术社区  · 16 年前

    我正在尝试使用java登录到报表系统。到目前为止,我已经尝试了许多使用HttpURLConnection的不同实现。我要发布的HTML可以归结为:

    <form name="logonForm" method="POST" action="http://remoteserver/logon.object">
    <input type="hidden" name="qryStr" value=""> 
    <input type="hidden" name="cmsVisible" value="true"> 
    <input type="hidden" name="authenticationVisible" value="true"> 
    <input type="hidden" name="referer" value="">
    <input type="hidden" name="refererFormData" value="">
    <input type="hidden" name="isFromLogonPage" value="true">
    <input type="text" name="cms" value="xxxxx" class="textfield" id="apsTextEdit">
    <input type="text" name="username" value="xxxxx"class="textfield" id="usernameTextEdit">
    <input type="text" name="password" value="xxxxx"class="textfield" id="passwordTextEdit">
    <select name="authType"id="authenticationSelectBox">
    <option selected value='xxxxx'>xxxxx</select>
    <input type="submit" name="SUBMIT">
    </form>
    

    我知道此表单提供有效的输入,因为当我在浏览器中单击“提交”时,它会将我登录到报表系统。但是,当我发送一个编程请求时,我总是将上面的html作为响应返回。注意,我已经尝试了大约一百万个使用HttpURLRequest的实现。举个例子:

            OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
            wr.write(parameters);
            wr.flush();
    
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            String allLines = "";
            while ((line = rd.readLine()) != null) {
                allLines += "\n" + line; // Process line...
            }
            wr.close();
            rd.close();
            return allLines;
    

    其中“参数”的范围从零到我发布的HTML中的每个URL编码参数,并且连接(当前,就像我尝试过的其他配置一样)设置如下:

            connection = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(true);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setAllowUserInteraction(false);
            connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=" + "UTF-8");
            connection.setUseCaches(false);
            connection.connect();
    

    我知道细节是有点稀疏,所以让我知道如果你需要任何更多的信息,非常感谢任何输入!

    2 回复  |  直到 16 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    你需要确保你也采取 name=value 成对的 全部的 <input type="hidden"> 元素和一 <input type="submit"> 您希望以编程方式在查询字符串中“按”。按钮只有一个名称,但无论如何,您需要传递它。通过这种方式,服务器端可以区分是否按下了按钮,如果是,则是哪个按钮。

    Set-Cookie 并将其设置为 Cookie

    为了更简单、更不冗长的HTTP/表单处理,请考虑 Apache HttpComponents Client .

    另请参见:

        2
  •  1
  •   ZZ Coder    16 年前

    参数必须正确编码。我通常放一张地图,然后像这样序列化,

                StringBuilder sb = new StringBuilder(baseUrl);
    
                for (Parameter p : parameters) {
                        if (sb.length() > 0)
                            sb.append('&');
                        try {
                                sb.append(URLEncoder.encode(p.name, "UTF-8"));
                                if (p.value != null) {
                                    sb.append('=');
                                    sb.append(URLEncoder.encode(p.value, "UTF-8"));
                                }
                        } catch (UnsupportedEncodingException e) {
                            // Not really possible, throw unchecked exception
                            throw new IllegalStateException("No UTF-8");
                        }
                }
    

    推荐文章