Java Html表单处理不返回输入的数据字符串

时间:2013-09-01 15:58:21

标签: java

尝试自动化这个html表单填充并返回下一页上的数据。传递的数据不会返回匹配站点中的内容的方式。 这是代码:

parseSite() throws Exception
{
     Map<String, String> data = new HashMap<>();
  data.put("txtEntryReg1", "35806573CB");
    data.put("txtEntryReg2", "35806573CB");
   data.put("t3", "8/30/2013 2:37:30 AM*");
   data.put("t3", "4:3844: 1NB/9143/21 *");

 data.put("btnSubmit2","+Print+Your+Payment+Invoice");
  //data.putAll("txtEntryReg1=35278160IG&txtEntryReg2=35278160IG&t3=4%3A3623%3A+5NB%2F9143%2F21+*&t3=8%2F30%2F2013+2%3A15%3A24+AM*&btnSubmit2=+Print+Your+Payment+Invoice");
     //data.putAll(data);

     //key1=value1&key2=value2&key3=value3

        doSubmit("http://poters.com.ng/mo/std/acp_slp/1/default.aszp", data);

}

    public void doSubmit(String url, Map<String, String> data) throws Exception {
    URL siteUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
           // conn.setRequestProperty("Cookie", "__utma=244564033.513877751.1371763186.1376824439.1376824439.39; __utmc=244564033; __utmz=244564033.1373732391.30.3.utmcsr=login.lionet50.net|utmccn=(referral)|utmcmd=referral|utmcct=/login");
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
            conn.setDoOutput(true);


    DataOutputStream out = new DataOutputStream(conn.getOutputStream());

    Set keys = data.keySet();
    Iterator keyIter = keys.iterator();
    String content = "";

            for(int i=0; keyIter.hasNext(); i++) {
        Object key = keyIter.next();
        if(i!=0) {
            content += "&";
        }
        content += key + "=" + URLEncoder.encode(data.get(key), "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();
}`

打印时的数据,即System.out.println(内容)。返回

&t3=4%3A3844%3A+1NB%2F9143%2F21+*&txtEntryReg2=35806573CB&btnSubmit2=%2BPrint%2BYour%2BPayment%2BInvoice

但我想要打印  txtEntryReg1=35278160IG&txtEntryReg2=35278160IG&t3=+%3A3112%3A+8NB%2F0%2F213422*&t3=9%2F1%2F2013+12%3A00%3A17+AM*&btnSubmit=+Create+Payment+Invoice

尝试更改data.put(...,...)订单但没有任何工作。 有人请帮忙

2 个答案:

答案 0 :(得分:1)

这是因为HashMap不保留记录的插入顺序。您可以改为使用LinkedHashMap

但是,为什么订单很重要?

答案 1 :(得分:0)

除了以下内容之外,您的代码中没有此类问题:

您将t3的值设置为两次,因此地图将覆盖它,您将只获得一个值。此外,LinkedHashMap将有助于维护订单。我不确定这是否是优先事项。

如果您担心编码的URL,请使用URL解码来获取您可以阅读的内容。这可以使用如下:

    URLDecoder.decode(encodedURL)