我正在使用HTMLUnit发送POST请求,该请求将关键字作为参数发送。 URL的一个示例是:
website.com/foo/bar/api?keywords=word1,word2,word3&language=en
问题是我的应用程序是动态选择这些单词,单词的数量可以达到10或20甚至更多。如何将一组单词作为值附加到HTTP请求。我的代码目前是:
requestSettings = new WebRequest(new URL("website.com/foo/bar/api?"),
HttpMethod.POST);
Iterator<String> itr = list.iterator();
while(itr.hasNext()) {
requestSettings.getRequestParameters()
.add(new NameValuePair("keywords[]", itr.next()));
}
requestSettings.getRequestParameters().add(new NameValuePair("language", "en"));
System.out.println(requestSettings.getUrl().toString());
response = webClient.getPage(requestSettings).getWebResponse();
此代码不会返回有效的响应。我在这里做错了什么?
答案 0 :(得分:1)
尝试一下:
using (var client = new WebClient())
{
var dataObject = new {
KeyWords = "one, two, three"
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(dataObject);
var response = client.UploadString("yourUrl", json);
}