使用Java发送HTTP请求GET / POST以形成? [英] Sending HTTP Request GET/POST to form with Java?

查看:169
本文介绍了使用Java发送HTTP请求GET / POST以形成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码,我得到它的工作,现在它基本上允许我发送http post并获取几乎任何我想要的外部网站的请求除非元素不包含name属性。这是一个例子:

So I have this piece of code, and I got it to work, and now it basically allows me to send http post and get requests to almost any external website I want UNLESS the elements don't contain a name attribute. Here's an example:

这是Java代码:

    public static String sendPostRequest(String url) {

    StringBuffer sb = null;

    try {

        String data = URLEncoder.encode("user", "UTF-8") + "="
                + URLEncoder.encode("myUserName", "UTF-8") + "&"
                + URLEncoder.encode("submit", "UTF-8") + "="
                + URLEncoder.encode("Submit", "UTF-8");


        URL requestUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) requestUrl
                .openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");

        OutputStreamWriter osw = new OutputStreamWriter(
                conn.getOutputStream());
        osw.write(data);
        osw.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));

        String in = "";
        sb = new StringBuffer();

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        osw.close();
        br.close();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sb.toString();
}

这是我正在尝试向其发送请求的表单(它是一个w3schools网站上的表格,这是 http://www.w3schools.com/html/html_forms .asp ):

This is the form I'm trying to send a request to (it's a form on the w3schools site, this being the site http://www.w3schools.com/html/html_forms.asp):

<form name="input0" target="_blank" action="html_form_action.asp" method="get">

Username: 

<input type="text" name="user" size="20" />

<input type="submit" value="Submit" />

</form>

现在因为提交按钮没有名称属性,我无法发送正确的HTTP获取/发布请求(我知道在这种情况下它是一个get方法)。如何使用(适当的键/值)替换String数据,以便它实际向此表单发送请求?

Now because the Submit button doesn't have a name attribute, I can't send a proper HTTP Get/Post request to it (I know it's a get method in this case). What do I replace the String data with (what proper keys/values) so that it actually sends a request to this form?

推荐答案

您不要将 submit 部分添加到您的数据中所有。这仅供浏览器知道提交按钮触发操作。请注意新打开的网站的网址如何显示: http://www.w3schools。 com / html / html_form_action.asp?user = myUserName - 此处没有提交部分。所以你的数据代码应如下所示:

You don't add the submit part to your data at all. This is just for the browser to know that "Submit" button triggers the action. Notice how the URL of the newly opened site looks: http://www.w3schools.com/html/html_form_action.asp?user=myUserName - no submit part here. So your data code should look like this:

String data = URLEncoder.encode("user", "UTF-8") + "="
            + URLEncoder.encode("myUserName", "UTF-8"); // end here

这篇关于使用Java发送HTTP请求GET / POST以形成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆