本文整理汇总了Java中org.apache.http.impl.client.BasicResponseHandler类的典型用法代码示例。如果您正苦于以下问题:Java BasicResponseHandler类的具体用法?Java BasicResponseHandler怎么用?Java BasicResponseHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BasicResponseHandler类属于org.apache.http.impl.client包,在下文中一共展示了BasicResponseHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: notifyHunter
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public String notifyHunter(byte[] content) throws IOException {
try {
String request = new String(content);
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
HttpClient httpclient = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpPost httpPost = new HttpPost("https://api"+hunterDomain.substring(hunterDomain.indexOf("."))+"/api/record_injection");
String json = "{\"request\": \""+request.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r\n", "\\n")+"\", \"owner_correlation_key\": \""+hunterKey+"\", \"injection_key\": \""+injectKey+"\"}";
StringEntity entity = new StringEntity(json);
entity.setContentType("applicaiton/json");
httpPost.setEntity(entity);
HttpResponse response = httpclient.execute(httpPost);
String responseString = new BasicResponseHandler().handleResponse(response);
return responseString;
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
Logger.getLogger(HunterRequest.class.getName()).log(Level.SEVERE, null, ex);
}
return "Error Notifying Probe Server!";
}
示例2: getResponseAsString
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public static Optional<String> getResponseAsString(HttpRequestBase httpRequest, HttpClient client) {
Optional<String> result = Optional.empty();
final int waitTime = 60000;
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(waitTime).setConnectTimeout(waitTime)
.setConnectionRequestTimeout(waitTime).build();
httpRequest.setConfig(requestConfig);
result = Optional.of(client.execute(httpRequest, responseHandler));
} catch (HttpResponseException httpResponseException) {
LOG.error("getResponseAsString(): caught 'HttpResponseException' while processing request <{}> :=> <{}>", httpRequest,
httpResponseException.getMessage());
} catch (IOException ioe) {
LOG.error("getResponseAsString(): caught 'IOException' while processing request <{}> :=> <{}>", httpRequest, ioe.getMessage());
} finally {
httpRequest.releaseConnection();
}
return result;
}
示例3: getToken
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public AccessTokenDTO getToken(String code) {
DefaultHttpClient httpclient = new DefaultHttpClient();
AccessTokenDTO token = null;
try {
HttpPost httppost = new HttpPost("https://api.twitch.tv/kraken/oauth2/token" +
"?client_id=" + Config.getCatalog().twitch.clientId +
"&client_secret=" + Config.getCatalog().twitch.clientSecret +
"&code=" + code +
"&grant_type=authorization_code" +
"&redirect_uri=" + Config.getCatalog().twitch.redirectUri);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
token = new Gson().fromJson(responseBody, AccessTokenDTO.class);
} catch (IOException e) {
TwasiLogger.log.error(e);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
httpclient.close();
}
return token;
}
示例4: queryConfig
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
/**
* 查询配置
*/
public Map<String, String> queryConfig() {
try {
String resultStr = HTTP_CLIENT.execute(request, new BasicResponseHandler());
FindPropertiesResult result = JSON.parseObject(resultStr, FindPropertiesResult.class);
if (result == null) {
throw new RuntimeException("请求配置中心失败");
}
if (!result.isSuccess()) {
throw new RuntimeException("从配置中心读取配置失败:" + result.getMessage());
}
return result.getProperties();
} catch (IOException e) {
return ExceptionUtils.rethrow(e);
}
}
示例5: getText
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public static String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
// httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
示例6: main
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println(responseBody);
System.out.println("----------------------------------------");
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
示例7: getResponseJsonObject
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
private JSONObject getResponseJsonObject(String httpMethod, String url, Object params) throws IOException, MtWmErrorException {
HttpUriRequest httpUriRequest = null;
String fullUrl = getBaseApiUrl() + url;
List<NameValuePair> sysNameValuePairs = getSysNameValuePairs(fullUrl, params);
List<NameValuePair> nameValuePairs = getNameValuePairs(params);
if (HTTP_METHOD_GET.equals(httpMethod)) {
sysNameValuePairs.addAll(nameValuePairs);
HttpGet httpGet = new HttpGet(fullUrl + "?" + URLEncodedUtils.format(sysNameValuePairs, UTF_8));
setRequestConfig(httpGet);
httpUriRequest = httpGet;
} else if (HTTP_METHOD_POST.equals(httpMethod)) {
HttpPost httpPost = new HttpPost(fullUrl + "?" + URLEncodedUtils.format(sysNameValuePairs, UTF_8));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, UTF_8));
setRequestConfig(httpPost);
httpUriRequest = httpPost;
}
CloseableHttpResponse response = this.httpClient.execute(httpUriRequest);
String resultContent = new BasicResponseHandler().handleResponse(response);
JSONObject jsonObject = JSON.parseObject(resultContent);
MtWmError error = MtWmError.fromJson(jsonObject);
if (error != null) {
logging(url, httpMethod, false, httpUriRequest.getURI() + "\nBody:" + JSON.toJSONString(params), resultContent);
throw new MtWmErrorException(error.getErrorCode(), error.getErrorMsg());
}
logging(url, httpMethod, true, httpUriRequest.getURI() + "\nBody:" + JSON.toJSONString(params), resultContent);
return jsonObject;
}
示例8: makeBasicPostRequest
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
private static String makeBasicPostRequest(String path, JSONObject jsonObject, String token) throws Exception {
DefaultHttpClient httpclient = getNewHttpClient();
HttpPost httpPost = new HttpPost(path);
StringEntity se = new StringEntity(jsonObject.toString());
httpPost.setEntity(se);
setBasicHeaders(httpPost, token);
//Handles what is returned from the page
ResponseHandler responseHandler = new BasicResponseHandler();
String response = "{\"success\":\"false\"}";
try {
response = (String) httpclient.execute(httpPost, responseHandler);
} catch (org.apache.http.client.HttpResponseException e) {
e.printStackTrace(); // todo: retrieve status code and evaluate it
Log.d("statusCode order Post", Integer.toString(e.getStatusCode()));
}
return response;
}
示例9: postImpex
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
/**
* Send HTTP POST request to {@link #getEndpointUrl(), imports impex
*
* @param file
* file to be imported
* @return import status message
* @throws IOException
* @throws HttpResponseException
*/
private String postImpex(final IFile file) throws HttpResponseException, IOException {
final Map<String, String> parameters = new HashMap<>();
final HttpPost postRequest = new HttpPost(getEndpointUrl() + ImpexImport.IMPEX_IMPORT_PATH);
final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(getTimeout()).build();
parameters.put(ImpexImport.Parameters.ENCODING, getEncoding());
parameters.put(ImpexImport.Parameters.SCRIPT_CONTENT, getContentOfFile(file));
parameters.put(ImpexImport.Parameters.MAX_THREADS, ImpexImport.Parameters.MAX_THREADS_VALUE);
parameters.put(ImpexImport.Parameters.VALIDATION_ENUM, ImpexImport.Parameters.VALIDATION_ENUM_VALUE);
postRequest.setConfig(requestConfig);
postRequest.addHeader(getxCsrfToken(), getCsrfToken());
postRequest.setEntity(new UrlEncodedFormEntity(createParametersList(parameters)));
final HttpResponse response = getHttpClient().execute(postRequest, getContext());
final String responseBody = new BasicResponseHandler().handleResponse(response);
return getImportStatus(responseBody);
}
示例10: fetchCsrfTokenFromHac
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
/**
* Send HTTP GET request to {@link #endpointUrl}, updates {@link #csrfToken}
* token
*
* @return true if {@link #endpointUrl} is accessible
* @throws IOException
* @throws ClientProtocolException
* @throws AuthenticationException
*/
protected void fetchCsrfTokenFromHac() throws ClientProtocolException, IOException, AuthenticationException {
final HttpGet getRequest = new HttpGet(getEndpointUrl());
try {
final HttpResponse response = httpClient.execute(getRequest, getContext());
final String responseString = new BasicResponseHandler().handleResponse(response);
csrfToken = getCsrfToken(responseString);
if( StringUtil.isBlank(csrfToken) ) {
throw new AuthenticationException(ErrorMessage.CSRF_TOKEN_CANNOT_BE_OBTAINED);
}
} catch (UnknownHostException error) {
final String errorMessage = error.getMessage();
final Matcher matcher = HACPreferenceConstants.HOST_REGEXP_PATTERN.matcher(getEndpointUrl());
if (matcher.find() && matcher.group(1).equals(errorMessage)) {
throw new UnknownHostException(
String.format(ErrorMessage.UNKNOWN_HOST_EXCEPTION_MESSAGE_FORMAT, matcher.group(1)));
}
throw error;
}
}
示例11: updateExternalIPAddr
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public static void updateExternalIPAddr() {
nu_log.i("Retrieving external IP address.");
CloseableHttpClient cli = HttpClients.createDefault();
nu_log.d("IP retrieval URL = " + ip_retrieval_url);
HttpGet req = new HttpGet(ip_retrieval_url);
req.addHeader("User-Agent", "PacChat HTTP " + Main.VERSION);
try {
CloseableHttpResponse res = cli.execute(req);
BasicResponseHandler handler = new BasicResponseHandler();
external_ip = handler.handleResponse(res);
nu_log.i("External IP address detected as: " + external_ip);
} catch (IOException e) {
nu_log.e("Error while retrieving external IP!");
e.printStackTrace();
}
}
示例12: getResponseByPost
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public String getResponseByPost(String url,String encode,String[] params)throws Exception{
httpPost = new HttpPost(url);
List<org.apache.http.NameValuePair> formParams = new ArrayList<org.apache.http.NameValuePair>();
for(int i=0; i<params.length/2;i++){
formParams.add(new BasicNameValuePair(params[i*2], params[i*2+1]));
}
HttpEntity entityForm = new UrlEncodedFormEntity(formParams, encode);
httpPost.setEntity(entityForm);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = httpClient.execute(httpPost, responseHandler);
return content;
}
示例13: getResponseByProxy
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
public String getResponseByProxy(String url)throws Exception{
httpClient = new DefaultHttpClient();
do{
HttpHost proxy = new HttpHost((String)getProxy().get(0), (Integer)getProxy().get(1));
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
httpGet = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
int count = 0;
try{
content = httpClient.execute(httpGet, responseHandler);
}catch(Exception e){
System.out.println("Remote accessed by proxy["+(String)getProxy().get(0)+":"+(Integer)getProxy().get(1)+"] had Error!Try next!");
}
count++;
if(count>2){break;}
}while(content.length()==0);
return content;
}
示例14: postLine
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
private void postLine(String data) throws Exception {
StringEntity postingString = new StringEntity(data);
//System.out.println(data);
httpPost.setEntity(postingString);
//httpPost.setHeader("Content-type","plain/text");
httpPost.setHeader("Content-type", "application/json");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String resp = httpClient.execute(httpPost, responseHandler);
//JSONObject jsonResp = new JSONObject(resp);
//System.out.println(jsonResp);
httpPost.releaseConnection();
}
示例15: getText
import org.apache.http.impl.client.BasicResponseHandler; //导入依赖的package包/类
private String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
httpclient.getConnectionManager().shutdown();
}
return responseBody;
}