如何使用urlConnection单击按钮(以编程方式)

时间:2012-03-05 21:03:19

标签: java httpurlconnection

我正在尝试访问内部网站项目,这是网站页面源的一部分

<form method="post" action="doDelete">
     Are you sure you want to delete 'Apple?'?
 <input name="Submit" value="Yes" class="submit-button" type="submit" />
 </form>

我有这段代码

  

doSubmit( “HTTP://ma.some-website:8080 /构件/苹果/删除”, “是”);

 public static void doSubmit(String url, String data) throws Exception {
    String content = "";
    URL siteUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    HttpURLConnection.setFollowRedirects( true );
    DataOutputStream out = new DataOutputStream(conn.getOutputStream());

        content =  URLEncoder.encode(data, "UTF-8");

    System.out.println(content);
    out.writeBytes(content);
    out.flush();
    out.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line = "";
    while((line=in.readLine())!=null) {
        System.out.println(line);
    }
    in.close();

}

在网页上有一个确认框询问“我想删除此用户(Apple)”如何以编程方式点击该按钮?我想我需要识别“doDelete”的方法并传入“Yes”参数?

1 个答案:

答案 0 :(得分:0)

你的问题有点令人困惑。您似乎试图通过进行http调用来模拟浏览器。我建议使用某种代理来记录您想要重现的http调用,然后您可以根据需要对它们进行编码。可能值得谷歌搜索框架,使这更容易。