本文整理匯總了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);
}
}