1

尝试自动填充此 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(content)。返回

&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(...,...) 顺序,但仍然没有任何效果。有人请帮忙

4

2 回答 2

1

这是因为 aHashMap不保留插入记录的顺序。您可以使用 aLinkedHashMap代替。

但是,为什么顺序很重要?

于 2013-09-01T16:20:27.617 回答
0

您的代码中没有此类问题,但以下情况除外:

您将值放置t3两次,因此地图将覆盖它,您将只获得一个值。此外,LinkedHashMap将有助于维持秩序。我不确定这是否是优先事项。

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

    URLDecoder.decode(encodedURL)
于 2013-09-01T16:26:20.703 回答