本文整理匯總了Java中org.apache.http.client.methods.HttpGet.setHeader方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpGet.setHeader方法的具體用法?Java HttpGet.setHeader怎麽用?Java HttpGet.setHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.methods.HttpGet
的用法示例。
在下文中一共展示了HttpGet.setHeader方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getRequest
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public static void getRequest(String type) {
HttpResponse httpResponse = null;
try {
if (type.equals("GET")) {
HttpGet httpGet = new HttpGet("");
;
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
httpResponse = client.execute(httpGet);
} else if (type.equals("POST")) {
HttpPost httpPost = new HttpPost();
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例2: getOcrInputStream
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
/**
* 獲取驗證碼圖片,直接返回圖片流
*/
public InputStream getOcrInputStream() {
HttpGet get = new HttpGet(Api.verifyCodeUrl + System.currentTimeMillis());
get.setHeader(new BasicHeader("Referer", "https://c.dcits.com/mydcitslogin.html"));// 這個頭必需
try {
HttpResponse resp = client.execute(get);
ocrStream = resp.getEntity().getContent();
logger.info("獲取驗證碼圖片成功!");
return ocrStream;
} catch (Exception e) {
logger.error("獲取驗證碼圖片異常:", e);
}
return null;
}
示例3: ticketQuery
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public static String ticketQuery(TrainQuery trainQuery) {
Objects.requireNonNull(trainQuery);
CloseableHttpClient httpClient = buildHttpClient();
HttpGet httpGet = new HttpGet(UrlConfig.ticketQuery + "?" + genQueryParam(trainQuery));
httpGet.setHeader(CookieManager.cookieHeader());
String result = StringUtils.EMPTY;
try(CloseableHttpResponse response = httpClient.execute(httpGet)) {
CookieManager.touch(response);
result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
logger.error("ticketQuery error", e);
}
return result;
}
示例4: fetchJweToken
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public JweToken fetchJweToken(String googleIdToken) throws IOException
{
CsrfToken csrfToken;
HttpGet csrfRequest = new HttpGet(k8sClusterEndpoint
+ "/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/api/v1/csrftoken/login");
csrfRequest.setHeader(HttpHeaders.AUTHORIZATION, BEARER_PREFIX + googleIdToken);
try (CloseableHttpResponse csrfResponse = httpClient.execute(csrfRequest)) {
csrfToken = objectMapper.readValue(csrfResponse.getEntity().getContent(), CsrfToken.class);
}
HttpPost jweRequest = new HttpPost(k8sClusterEndpoint
+ "/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/api/v1/login");
jweRequest.setHeader(HttpHeaders.AUTHORIZATION, BEARER_PREFIX + googleIdToken);
jweRequest.setHeader("x-csrf-token", csrfToken.getToken());
jweRequest.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
jweRequest.setEntity(new StringEntity(objectMapper.writeValueAsString(new JweTokenRequestBody().withToken(googleIdToken))));
try (CloseableHttpResponse jweResponse = httpClient.execute(jweRequest)) {
JweToken jweToken = objectMapper.readValue(IOUtils.toString(jweResponse.getEntity().getContent(),
StandardCharsets.UTF_8),
JweToken.class);
jweToken.setEncodedJweToken(URLEncoder.encode(jweToken.getJweToken(), StandardCharsets.UTF_8.toString()));
return jweToken;
}
}
示例5: parse
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public static List<ThreadPost> parse(String[] param) throws Exception {
HttpClientBuilder builder = HttpClientBuilder.create();
try (CloseableHttpClient client = builder.build()) {
StringBuilder url = new StringBuilder("http://www.mcbbs.net/forum.php?");
for (String para : param)
url.append(para).append("&");
url.append("mod=forumdisplay&mobile=2");
HttpGet get = new HttpGet(url.toString());
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000).setSocketTimeout(2000).build();
get.setConfig(requestConfig);
get.setHeader("User-Agent",
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Mobile Safari/537.36");
get.setHeader("Connection", "keep-alive");
try (CloseableHttpResponse response = client.execute(get)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, "utf-8");
return parseXml(html);
}
}
}
return new ArrayList<>();
}
示例6: getKeys
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
@Override
public String getKeys() throws IOException
{
HttpGet get = new HttpGet(_jwksUri);
get.setHeader(ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
HttpResponse response = _httpClient.execute(get);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
{
_logger.severe(() -> "Got error from Jwks server: " + response.getStatusLine().getStatusCode());
throw new IOException("Got error from Jwks server: " + response.getStatusLine().getStatusCode());
}
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
示例7: checkProxy
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
@Override
public boolean checkProxy(Proxy proxy){
HttpGet httpGet = new HttpGet("http://icanhazip.com/");
CloseableHttpResponse response = null;
RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(proxy.getIp(),proxy.getPort())).build();
httpGet.setConfig(requestConfig);
httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
try {
response = this.httpClient.execute(httpGet);
String content = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
response.close();
return proxy.getIp().equals(content.trim());
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
示例8: download
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
@Override
public String download(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
for(Map.Entry<String,String> header : this.headers().entrySet()){
httpGet.setHeader(header.getKey(),header.getValue());
}
CloseableHttpResponse response = this.httpClient.execute(httpGet);
String content = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
response.close();
return content;
}
示例9: httpNotification
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
private static int httpNotification(String uri, int timeout)
throws IOException, URISyntaxException {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams()
.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout)
.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, (long) timeout);
HttpGet httpGet = new HttpGet(new URI(uri));
httpGet.setHeader("Accept", "*/*");
return client.execute(httpGet).getStatusLine().getStatusCode();
}
示例10: call
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public List<HouseInfo> call() {
lstHouseInfo = new ArrayList<HouseInfo>();
CloseableHttpClient client = HttpClients.createDefault();
Lock lock = new ReentrantLock();
HttpGet get = new HttpGet(url);
//設置請求頭
get.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
get.setHeader("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.8, en-US; q=0.5, en; q=0.3");
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
try{
//獲取響應
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, "UTF-8");
//解析html
Document doc = Jsoup.parse(html);
Elements lists = doc.getElementsByAttribute("logr");
lock.lock();
try{
for (Element list : lists) {
//將關鍵信息提取出來
HouseInfo houseInfo = new HouseInfo();
String[] houseInfoArray = list.getElementsByTag("h2").first().text().split(" ");
houseInfo.setHouseTitle(list.getElementsByTag("h2").first().text());
houseInfo.setHouseURL("http://" + "bj" + ".58.com" + list.getElementsByTag("a").first().attributes().get("href"));
houseInfo.setMoney(list.getElementsByClass("money").tagName("b").text());
houseInfo.setHouseLocation(houseInfoArray[1]);
lstHouseInfo.add(houseInfo);
}
}finally {
lock.unlock();
}
}catch (IOException e){
System.out.println( e.getMessage());
}
return lstHouseInfo;
}
示例11: testCorsXHRRequestFromWhitelistedOriginForNonSwaggerUIApi
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
@Test
public void testCorsXHRRequestFromWhitelistedOriginForNonSwaggerUIApi() throws Exception {
HttpGet request = new HttpGet(this.acsBaseUrl + "/acs");
request.setHeader(HttpHeaders.ORIGIN, "http://someone.predix.io");
request.setHeader("X-Requested-With", "true");
HttpResponse response = client.execute(request);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
System.out
.println("Access-Control-Allow-Origin : " + response.getHeaders("Access-Control-Allow-Origin").length);
Assert.assertEquals(response.getStatusLine().getStatusCode(), 403);
Assert.assertFalse(response.containsHeader("Access-Control-Allow-Origin"));
}
示例12: main
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public static void main(String[] args) {
try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://api.live.bilibili.com/User/getUserInfo");
System.out.println("Cookies:");
httpGet.setHeader("Cookie", new Scanner(System.in).nextLine());
UserInfoEntity userInfoEntity = JSON.parseObject(EntityUtils.toString(closeableHttpClient.execute(httpGet).getEntity()), UserInfoEntity.class);
System.out.println(JSON.toJSONString(userInfoEntity));
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: doGet
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
/**
* Executes get http request to an endpoint with provided headers.
*
* @param uri
* Endpoint that needs to be hit
* @param headers
* Key value pair of headers
* @return Return response body after executing GET
* @throws StockException
* if api doesn't return with success code or when null/empty
* endpoint is passed in uri
*/
public static String doGet(final String uri,
final Map<String, String> headers) throws StockException {
if (sHttpClient == null) {
sHttpClient = HttpUtils.initialize();
}
HttpResponse response = null;
String responseBody = null;
HttpGet request = new HttpGet();
if (uri == null || uri.isEmpty()) {
throw new StockException(-1, "URI cannot be null or Empty");
}
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
request.setHeader(entry.getKey(), entry.getValue());
}
}
try {
request.setURI(new URI(uri));
response = sHttpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
responseBody = EntityUtils.toString(response.getEntity());
} else if (response.getStatusLine().getStatusCode()
== HttpStatus.SC_NO_CONTENT) {
responseBody = String.valueOf(HttpStatus.SC_NO_CONTENT);
} else if (response.getStatusLine().getStatusCode()
/ HTTP_STATUS_CODE_DIVISOR
== HTTP_STATUS_CODE_API_ERROR) {
responseBody = EntityUtils.toString(response.getEntity());
throw new StockException(response.getStatusLine()
.getStatusCode(), responseBody);
} else if (response.getStatusLine().getStatusCode()
/ HTTP_STATUS_CODE_DIVISOR == HTTP_STATUS_CODE_REDIRECT) {
String locationHeader =
response.getHeaders("Location")[0].getValue();
responseBody = locationHeader;
} else if (response.getStatusLine().getStatusCode()
/ HTTP_STATUS_CODE_DIVISOR
== HTTP_STATUS_CODE_SERVER_ERROR) {
throw new StockException(response.getStatusLine()
.getStatusCode(), "API returned with Server Error");
}
} catch (StockException se) {
throw se;
} catch (Exception ex) {
throw new StockException(ex.getMessage());
}
return responseBody;
}
示例14: getStatus
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
/**
* Post json message broadcast.
*
* @return the string
*/
public static String getStatus() {
HttpGet get = new HttpGet(RasaBotMillContext.getRasaConfig().toString() + NetworkConstants.STATUS_EP + concatToken());
get.setHeader("Content-Type", "application/json");
return send(get);
}
示例15: downloadAPI
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public static HttpResult downloadAPI(Map<String, String> params, String remoteServiceUrl, String cookie) {
HttpResult r = new HttpResult();
// 請求參數
List<NameValuePair> list = new ArrayList<NameValuePair>();
if (params != null) {
for (String key : params.keySet()) {
list.add(new BasicNameValuePair(key, params.get(key)));
}
}
InputStream inputStream = null;
try {
HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
HttpGet post = new HttpGet(remoteServiceUrl);
if (cookie != null) {
post.setHeader("Cookie", cookie);
}
// post.setEntity(entity);
DefaultHttpClient client = new DefaultHttpClient();
// 發送Http請求並獲取響應
HttpResponse response = client.execute(post);
Header[] cookies = response.getHeaders("Set-Cookie");
if (cookies.length > 0) {
r.cookie = cookies[0].getValue();
}
// String cookie= .getValue();
if (response.getStatusLine().getStatusCode() == 200) {
// 獲取響應的消息實體
entity = response.getEntity();
// 獲取輸入流
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = reader.readLine()) != null) {
r.result += line;
}
}
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return r;
}