通过Java中的DataOutputStream发送多个POST请求 [英] Send Multiple POST Requests Through a DataOutputStream in Java
问题描述
我正在尝试使用for循环通过DataOutputStream发送多个POST请求,然后将其关闭.目前,只有"交易"数组列表的第一个索引发送到网站.任何其他索引都将被忽略,我假设它们没有被发送.我想知道我是否正确冲洗了水流吗?谢谢!!!
I am trying to use a for loop to send multiple POST requests through a DataOutputStream and then close it. At the moment, only the first index of the "trades" array list is sent to the website. Any other indexes are ignored and I'm assuming they are not being sent. I wonder if I am properly flushing the stream? Thank you!!!
交易值的示例:"101841599","101841801"
Examples of trades values: "101841599", "101841801"
代码值的示例:85e4c22
Example of code value: 85e4c22
我的代码段:
private ArrayList<String> trades = new ArrayList<String>();
private String code;
String url = "http://www.dota2lounge.com/ajax/bumpTrade.php";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
con.setRequestProperty("Cookie", cookie);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
for(int i=0; i<trades.size(); i++){
wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes("trade=" + trades.get(i) + "&code=" + code);
wr.flush();
System.out.println("again");
}
wr.flush();
wr.close();
推荐答案
事实证明,在开始新的连接之前,我实际上必须获得响应才能正确关闭连接.将这些行添加到for循环的末尾可解决此问题:
It turns out I had to actually get the response for it to properly close the connection before I started a new one. Appending these lines to the end of the for loop fixed the issue:
int nothing = con.getResponseCode();
String morenothing = con.getResponseMessage();
这篇关于通过Java中的DataOutputStream发送多个POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!