java 使用 HTTPURLConnection 提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15805771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
submit form using HTTPURLConnection
提问by user1107753
Hi I am trying to submit the following form using HTTPURLConnection as a exercise.
嗨,我正在尝试使用 HTTPURLConnection 作为练习提交以下表单。
<form name="popnames" method="post" action="/cgi-bin/popularnames.cgi" onsubmit="return submitIt();">
<p>
<label for="year">Birth Year:</label><br>
<input type="text" name="year" size="5" maxlength="4" id="year" value="2011">
</p>
<p>
<label for="rank">Popularity:</label><br>
<select name="top" size="1" id="rank">
<option value="20">Top 20</option>
<option value="50">Top 50</option>
<option value="100">Top 100</option>
<option value="500">Top 500</option>
<option value="1000">Top 1000</option>
</select>
</p>
<fieldset>
<legend>Name rankings may include:</legend>
<input type="radio" name="number" value="p" id="percent">
<label for="percent">Percent of total births</label><br>
<input type="radio" name="number" value="n" id="number">
<label for="number">Number of births</label>
</fieldset>
<hr>
<input class="uef-btn uef-btn-primary" type="submit" value=" Go ">
</form>
I am using HTTPURLConnection to do the submit This is my code and my test class
我正在使用 HTTPURLConnection 进行提交 这是我的代码和我的测试类
public class FormSubmitServiceTest {
@Test
public void testSubmit() throws Exception {
String url = "http://www.socialsecurity.gov/OACT/babynames/#ht=1";
Map<String, String> data = new HashMap<String, String>();
data.put("year", "2010");
data.put("top", "50");
data.put("number", "n");
FormSubmitService service = new FormSubmitService();
service.doSubmit(url, data);
}
}
And my service class which does the work
还有我的服务班
public class FormSubmitService {
public void doSubmit(String url, Map<String, String> data) throws IOException {
URL siteUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setUseCaches (true);
conn.setDoOutput(true);
conn.setDoInput(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();
}
}
Can anybody advise why this does not work in submitting the form. Is it because I am not clicking the submit button with a value of GO. And if that is the case then how do i actually click it because I would expect to send a name value pair across but the submit button does not have a name only a value.
任何人都可以建议为什么这在提交表单时不起作用。是不是因为我没有点击值为 GO 的提交按钮。如果是这种情况,那么我如何实际单击它,因为我希望发送一个名称值对,但提交按钮没有名称,只有一个值。
When I post the form from this code I expect the response to contain the same data as when I do the form submission manually which is the data on this page http://www.socialsecurity.gov/cgi-bin/popularnames.cgi.
当我从此代码发布表单时,我希望响应包含与我手动提交表单时相同的数据,即此页面http://www.socialsecurity.gov/cgi-bin/popularnames.cgi上的数据。
However on running the test class the data in the response I get is the same as the original page http://www.socialsecurity.gov/OACT/babynames/#ht=1.
但是,在运行测试类时,我得到的响应中的数据与原始页面http://www.socialsecurity.gov/OACT/babynames/#ht=1相同。
All help appreciated
感谢所有帮助
Thanks
谢谢
采纳答案by Friso
I think that you'll notice that if you change the URL in your testcase to http://www.socialsecurity.gov/cgi-bin/popularnames.cgiit'll all work.
我想您会注意到,如果您将测试用例中的 URL 更改为http://www.socialsecurity.gov/cgi-bin/popularnames.cgi,一切都会起作用。