本文整理汇总了Java中com.gargoylesoftware.htmlunit.html.HtmlPage.getWebResponse方法的典型用法代码示例。如果您正苦于以下问题:Java HtmlPage.getWebResponse方法的具体用法?Java HtmlPage.getWebResponse怎么用?Java HtmlPage.getWebResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.gargoylesoftware.htmlunit.html.HtmlPage
的用法示例。
在下文中一共展示了HtmlPage.getWebResponse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFormHeaderOk
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
/**
* Checks that CSRF validation works if token sent as header instead of
* form field.
*
* @throws Exception an error occurs or validation fails.
*/
@Test
public void testFormHeaderOk() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");
// Check response and CSRF header
WebResponse res = page1.getWebResponse();
assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
assertNotNull(res.getResponseHeaderValue(CSRF_HEADER));
WebRequest req = new WebRequest(new URL(webUrl + "resources/csrf"));
req.setHttpMethod(HttpMethod.POST);
req.setAdditionalHeader(CSRF_HEADER, res.getResponseHeaderValue(CSRF_HEADER));
res = webClient.loadWebResponse(req);
assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
}
示例2: download
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的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);
}
}