当前位置: 首页 > 工具软件 > HtmlUnit > 使用案例 >

htmlunit 发http请求

戎志勇
2023-12-01
	public static WebClient createWebClient(String ip, String port) {
		WebClient client = null;
		try {
			if (StringUtils.isBlank(ip) || StringUtils.isBlank(port)) {
				client = new WebClient(BrowserVersion.FIREFOX_24);
			} else {
				client = new WebClient(BrowserVersion.FIREFOX_24, ip,
						Integer.valueOf(port));
			}
			client.getOptions().setUseInsecureSSL(true);
			client.getOptions().setCssEnabled(false);
			client.getOptions().setJavaScriptEnabled(false);
		} catch (Exception e) {
			log.error(e);
		}
		return client;
	}

	public static String htmlUnitUrl(String url, WebClient webClient) {
		try {
			WebRequest request = new WebRequest(new URL(url), HttpMethod.GET);
			Map<String, String> additionalHeaders = new HashMap<String, String>();
			additionalHeaders
					.put("User-Agent",
							"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");
			additionalHeaders.put("Accept-Language", "zh-CN,zh;q=0.8");
			additionalHeaders.put("Accept", "*/*");
						request.setAdditionalHeaders(additionalHeaders);
			// 获取某网站页面
			Page page = webClient.getPage(request);
			// System.out.println(Page.getWebResponse().getContentAsString());
			return page.getWebResponse().getContentAsString();
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

 

 类似资料: