本文整理汇总了Java中com.gargoylesoftware.htmlunit.WebRequest.setRequestParameters方法的典型用法代码示例。如果您正苦于以下问题:Java WebRequest.setRequestParameters方法的具体用法?Java WebRequest.setRequestParameters怎么用?Java WebRequest.setRequestParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.gargoylesoftware.htmlunit.WebRequest
的用法示例。
在下文中一共展示了WebRequest.setRequestParameters方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loginBySpecialPost
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
protected RespWithTokenJSON loginBySpecialPost() throws FailingHttpStatusCodeException, IOException {
WebRequest wr = new WebRequest(new URL(MP_WEIXIN_LOGIN), HttpMethod.POST);
wr.getAdditionalHeaders().put("Accept", "*/*");
wr.getAdditionalHeaders().put("Accept-Encoding", "gzip, deflate, br");
wr.getAdditionalHeaders().put("Accept-Language", "en-US,en;q=0.5");
wr.getAdditionalHeaders().put("Connection", "keep-alive");
wr.getAdditionalHeaders().put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
wr.getAdditionalHeaders().put("Host", "mp.weixin.qq.com");
wr.getAdditionalHeaders().put("Referer", "https://mp.weixin.qq.com/");
wr.getAdditionalHeaders().put("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
wr.getAdditionalHeaders().put("X-Requested-With", "XMLHttpRequest");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new NameValuePair("f", "json"));
params.add(new NameValuePair("imgcode", ""));
//here password should be encrypt by MD5 and to lower case
params.add(new NameValuePair("pwd", this.securityNP.getPass()));
params.add(new NameValuePair("username", this.securityNP.getName()));
wr.setRequestParameters(params);
Page page = this.webClient.getPage(wr);
String respJson = page.getWebResponse().getContentAsString();
return RespWithTokenJSON.fromJson(respJson);
}
示例2: login
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
private static List<HtmlAnchor> login(final WebClient client)
throws IOException, MalformedURLException, Exception {
final HtmlPage homepage = client.getPage("http://ogame.org");
// we use a POST because filling out the form and clicking the login button doesn't work...
WebRequest settings = new WebRequest(new URL(
"http://ogame.org/main/login"), HttpMethod.POST);
settings.setRequestParameters(new ArrayList<NameValuePair>());
settings.getRequestParameters().add(new NameValuePair("kid", ""));
settings.getRequestParameters().add(new NameValuePair("uni", Main.settings.get("universe")));
settings.getRequestParameters().add(new NameValuePair("login", Main.settings.get("login")));
settings.getRequestParameters().add(new NameValuePair("pass", Main.settings.get("password")));
page = client.getPage(settings);
updateGame(page);
List<HtmlAnchor> menu = (List<HtmlAnchor>)page.getByXPath("//a[contains(@class, 'menubutton')]");
//System.out.println(menu);
//dump(page, "game.html");
return menu;
}
示例3: execute
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context)
{
LoggerFactory.getLogger(Request.class).debug("Executing action: " + this);
try
{
WebRequest webRequest = new WebRequest(UrlUtils.toUrlUnsafe(Util.replacePlaceholders(url, context)), method);
List<NameValuePair> replacedParams = new ArrayList<>();
params.forEach(param -> replacedParams.add(new NameValuePair(param.getName(), param.getValue())));
webRequest.setRequestParameters(replacedParams);
if(credentials != null)
webRequest.setCredentials(new UsernamePasswordCredentials(credentials.getName(), credentials.getValue()));
context.getWebClient().getPage(webRequest);
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
示例4: createRequest
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
protected WebRequest createRequest(String requestUrl, Map<String, String> addtionalHeaders,
List<NameValuePair> params) throws MalformedURLException {
WebRequest wr = new WebRequest(new URL(requestUrl), HttpMethod.POST);
wr.getAdditionalHeaders().putAll(addtionalHeaders);
wr.setRequestParameters(params);
return wr;
}
示例5: testGetToken
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
public void testGetToken() throws FailingHttpStatusCodeException, IOException {
WebClient wc = weiWS.getInstance();
WebRequest wr = new WebRequest(new URL("https://mp.weixin.qq.com/cgi-bin/login"), HttpMethod.POST);
wr.getAdditionalHeaders().put("Accept", "*/*");
wr.getAdditionalHeaders().put("Accept-Encoding", "gzip, deflate, br");
wr.getAdditionalHeaders().put("Accept-Language", "en-US,en;q=0.5");
wr.getAdditionalHeaders().put("Connection", "keep-alive");
// wr.getAdditionalHeaders().put("Content-Length", "86");
wr.getAdditionalHeaders().put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
wr.getAdditionalHeaders().put("Host", "mp.weixin.qq.com");
wr.getAdditionalHeaders().put("Referer", "https://mp.weixin.qq.com/");
wr.getAdditionalHeaders().put("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
wr.getAdditionalHeaders().put("X-Requested-With", "XMLHttpRequest");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new NameValuePair("f", "json"));
params.add(new NameValuePair("imgcode", ""));
//3BCEBC34A480F472E27372F9DE2D5592
params.add(new NameValuePair("pwd", "3bcebc34a480f472e27372f9de2d5592"));
params.add(new NameValuePair("username", "[email protected]"));
wr.setRequestParameters(params);
Page page = wc.getPage(wr);
String response = page.getWebResponse().getContentAsString();
System.out.println(response);
Set<Cookie> cookies = weiWS.getInstance().getCookieManager().getCookies();
for (Cookie cookie : cookies) {
System.out.println(cookie.toString());
}
Gson gson = new Gson();
RespWithTokenJSON jsonObj = gson.fromJson(response, RespWithTokenJSON.class);
String homePath = jsonObj.getRedirect_url();
HtmlPage homePage = wc.getPage("https://mp.weixin.qq.com/" + homePath);
HtmlAnchor logout = (HtmlAnchor) homePage.getElementById("logout");
System.out.println(logout);
HtmlPage loginPage = wc.getPage("https://mp.weixin.qq.com");
System.out.println(loginPage.getUrl());
}
示例6: openWithoutLogin
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
public static LoginPage<DrawPage> openWithoutLogin(WebClient webClient, URL url, LocalDate parse) throws IOException {
WebRequest request = new WebRequest(new URL(url.toString() + "/draw"), HttpMethod.POST);
List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new NameValuePair("date", "2015-01-01"));
request.setRequestParameters(parameters);
return new LoginPage<>(webClient.getPage(request), DrawPage.class);
}
示例7: getHTMLPageWithPost
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
public static Document getHTMLPageWithPost(String url, ArrayList<NameValuePair> post) throws Exception {
Exception ex = null;
for (int i = 0; i < NUMBER_OF_TRIES; i++) {
try (WebClient webClient = createWebClient(url)) {
WebRequest requestSettings = new WebRequest(new URL(url), HttpMethod.POST);
requestSettings.setRequestParameters(new ArrayList<NameValuePair>());
requestSettings.getRequestParameters().addAll(post);
HtmlPage page = webClient.getPage(requestSettings);
JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
while (manager.getJobCount() > 0) {
Thread.sleep(500);
}
return Jsoup.parse(((HtmlPage) webClient.getCurrentWindow().getEnclosedPage()).asXml());
} catch (Exception e) {
System.err.println("Try " + (i + 1) + " of " + NUMBER_OF_TRIES
+ ". Link: " + url + ". Error: " + e.getMessage());
ex = e;
}
}
throw ex;
}
示例8: executeServerQuery
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
/**
* Submit a query to developer/QueryHandler, parse the JSON and return it.
*/
public static QueryHandler.ResultData executeServerQuery(final String query) throws IOException, SAXException {
final WebClient conversation = getConversation();
final URL url = new URL(TestUtils.URL_ROOT
+ "developer/QueryHandler");
final WebRequest request = new WebRequest(url);
request.setRequestParameters(Collections.singletonList(new NameValuePair(QueryHandler.QUERY_PARAMETER, query)));
final Page response = loadPage(conversation, request);
final String contentType = response.getWebResponse().getContentType();
if (!"application/json".equals(contentType)) {
final String text = getPageSource(response);
final File output = File.createTempFile("json-error", ".html", new File("screenshots"));
final FileWriter writer = new FileWriter(output);
writer.write(text);
writer.close();
Assert.fail("Error JSON from QueryHandler: "
+ response.getUrl() + " Contents of error page written to: " + output.getAbsolutePath());
}
final String responseData = getPageSource(response);
final ObjectMapper jsonMapper = new ObjectMapper();
QueryHandler.ResultData result = jsonMapper.readValue(responseData, QueryHandler.ResultData.class);
Assert.assertNull("SQL Error: "
+ result.getError(), result.getError());
return result;
}
示例9: testParameter
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
/**
* Verify RunSelectorParameter works via HTML form, http POST and CLI.
*/
@Test
public void testParameter() throws Exception {
FreeStyleProject job = rule.createFreeStyleProject();
job.addProperty(new ParametersDefinitionProperty(
new RunSelectorParameter("SELECTOR", new StatusRunSelector(StatusRunSelector.BuildStatus.SUCCESSFUL), "foo")));
CaptureEnvironmentBuilder ceb = new CaptureEnvironmentBuilder();
job.getBuildersList().add(ceb);
// Run via UI (HTML form)
WebClient wc = rule.createWebClient();
WebClientOptions wco = wc.getOptions();
// Jenkins sends 405 response for GET of build page.. deal with that:
wco.setThrowExceptionOnFailingStatusCode(false);
wco.setPrintContentOnFailingStatusCode(false);
HtmlForm form = wc.getPage(job, "build").getFormByName("parameters");
form.getSelectByName("").getOptionByText("Specified by build number").setSelected(true);
wc.waitForBackgroundJavaScript(10000);
form.getInputByName("_.buildNumber").setValueAttribute("6");
rule.submit(form);
Queue.Item q = rule.jenkins.getQueue().getItem(job);
if (q != null) q.getFuture().get();
while (job.getLastBuild().isBuilding()) Thread.sleep(100);
assertEquals("<BuildNumberRunSelector><buildNumber>6</buildNumber></BuildNumberRunSelector>",
ceb.getEnvVars().get("SELECTOR").replaceAll("\\s+", ""));
job.getBuildersList().replace(ceb = new CaptureEnvironmentBuilder());
// Run via HTTP POST (buildWithParameters)
WebRequest post = new WebRequest(
new URL(rule.getURL(), job.getUrl() + "/buildWithParameters"), HttpMethod.POST);
wc.addCrumb(post);
String xml = "<StatusRunSelector><buildStatus>STABLE</buildStatus></StatusRunSelector>";
post.setRequestParameters(Arrays.asList(new NameValuePair("SELECTOR", xml),
post.getRequestParameters().get(0)));
wc.getPage(post);
q = rule.jenkins.getQueue().getItem(job);
if (q != null) q.getFuture().get();
while (job.getLastBuild().isBuilding()) Thread.sleep(100);
assertEquals(xml, ceb.getEnvVars().get("SELECTOR"));
job.getBuildersList().replace(ceb = new CaptureEnvironmentBuilder());
// Run via CLI
CLI cli = new CLI(rule.getURL());
assertEquals(0, cli.execute(
"build", job.getFullName(), "-p", "SELECTOR=<StatusRunSelector/>"));
q = rule.jenkins.getQueue().getItem(job);
if (q != null) q.getFuture().get();
while (job.getLastBuild().isBuilding()) Thread.sleep(100);
assertEquals("<StatusRunSelector/>", ceb.getEnvVars().get("SELECTOR"));
}
示例10: download
import com.gargoylesoftware.htmlunit.WebRequest; //导入方法依赖的package包/类
public HttpResponse download(HttpRequest request, int timeout) throws DownloadException {
try {
URL url = new URL(request.getUrl());
WebRequest webRequest = new WebRequest(url);
webRequest.setHttpMethod(HttpMethod.GET);
if(request instanceof HttpPostRequest) {//post
HttpPostRequest post = (HttpPostRequest)request;
webRequest.setHttpMethod(HttpMethod.POST);
List<NameValuePair> requestParameters = new ArrayList<NameValuePair>();
for(Map.Entry<String, Object> entry : post.getFields().entrySet()) {
NameValuePair nvp = new NameValuePair(entry.getKey(), entry.getValue().toString());
requestParameters.add(nvp);
}
webRequest.setRequestParameters(requestParameters);
}
//header
boolean isMobile = SpiderThreadLocal.get().getEngine().isMobile();
webRequest.setAdditionalHeader("User-Agent", UserAgent.getUserAgent(isMobile));
webRequest.setAdditionalHeaders(request.getHeaders());
//proxy
HttpHost proxy = Proxys.getProxy();
if(proxy != null) {
webRequest.setProxyHost(proxy.getHostName());
webRequest.setProxyPort(proxy.getPort());
}
//timeout
this.webClient.getOptions().setTimeout(timeout);
//request,response
webClient.getPage(webRequest);
HtmlPage page = webClient.getPage(request.getUrl());
HttpResponse resp = new HttpResponse();
WebResponse webResponse = page.getWebResponse();
int status = webResponse.getStatusCode();
resp.setStatus(status);
if(status == 302 || status == 301) {
String redirectUrl = webResponse.getResponseHeaderValue("Location");
resp.setContent(UrlUtils.relative2Absolute(request.getUrl(), redirectUrl));
} else if(status == 200) {
String content = page.asXml();
resp.setContent(content);
resp.setRaw(webResponse.getContentAsStream());
String contentType = webResponse.getContentType();
resp.setContentType(contentType);
String charset = getCharset(request.getCharset(), contentType);
resp.setCharset(charset);
} else {
throw new DownloadException("ERROR : " + status);
}
return resp;
} catch(Exception ex) {
throw new DownloadException(ex);
}
}