本文整理汇总了Java中org.apache.http.Consts类的典型用法代码示例。如果您正苦于以下问题:Java Consts类的具体用法?Java Consts怎么用?Java Consts使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Consts类属于org.apache.http包,在下文中一共展示了Consts类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: login
import org.apache.http.Consts; //导入依赖的package包/类
public static String login(String randCode) {
CloseableHttpClient httpClient = buildHttpClient();
HttpPost httpPost = new HttpPost(UrlConfig.loginUrl);
httpPost.addHeader(CookieManager.cookieHeader());
String param = "username=" + encode(UserConfig.username) + "&password=" + encode(UserConfig.password) + "&appid=otn";
httpPost.setEntity(new StringEntity(param, ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)));
String result = StringUtils.EMPTY;
try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
result = EntityUtils.toString(response.getEntity());
CookieManager.touch(response);
ResultManager.touch(result, new ResultKey("uamtk", "uamtk"));
} catch (IOException e) {
logger.error("login error", e);
}
return result;
}
示例2: checkOrderInfo
import org.apache.http.Consts; //导入依赖的package包/类
public static String checkOrderInfo(TrainQuery query) {
CloseableHttpClient httpClient = buildHttpClient();
HttpPost httpPost = new HttpPost(UrlConfig.checkOrderInfo);
httpPost.addHeader(CookieManager.cookieHeader());
httpPost.setEntity(new StringEntity(genCheckOrderInfoParam(query), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)));
String result = StringUtils.EMPTY;
try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
CookieManager.touch(response);
result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
logger.error("checkUser error", e);
}
return result;
}
示例3: handle
import org.apache.http.Consts; //导入依赖的package包/类
@Override
public void handle(HttpExchange httpExchange) throws IOException {
StringBuilder body = new StringBuilder();
try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) {
char[] buffer = new char[256];
int read;
while ((read = reader.read(buffer)) != -1) {
body.append(buffer, 0, read);
}
}
Headers requestHeaders = httpExchange.getRequestHeaders();
Headers responseHeaders = httpExchange.getResponseHeaders();
for (Map.Entry<String, List<String>> header : requestHeaders.entrySet()) {
responseHeaders.put(header.getKey(), header.getValue());
}
httpExchange.getRequestBody().close();
httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length());
if (body.length() > 0) {
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(body.toString().getBytes(Consts.UTF_8));
}
}
httpExchange.close();
}
示例4: executeRequest
import org.apache.http.Consts; //导入依赖的package包/类
private String executeRequest(String url, String requestStr) throws WxErrorException {
HttpPost httpPost = new HttpPost(url);
if (this.wxMpService.getHttpProxy() != null) {
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
}
try (CloseableHttpClient httpclient = HttpClients.custom().build()) {
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result);
return result;
}
} catch (IOException e) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e);
} finally {
httpPost.releaseConnection();
}
}
示例5: report
import org.apache.http.Consts; //导入依赖的package包/类
/**
* 报工
*
*/
public boolean report() {
HttpPost post = new HttpPost(Api.reportUrl);
try {
post.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
HttpResponse resp = client.execute(post);
JSONObject jo = JSONObject.parseObject(EntityUtils.toString(resp.getEntity()));
// 报工成功,返回json结构的报文{"data" : [ {},{}...],"success" : true}
if (jo.getBooleanValue("success")) {
return true;
}
logger.warn(jo.getString("error"));
} catch (Exception e) {
logger.error("报工异常:", e);
}
return false;
}
示例6: executeAndGet
import org.apache.http.Consts; //导入依赖的package包/类
public String executeAndGet(HttpRequestBase httpRequestBase) throws Exception {
HttpResponse response;
String entiStr = "";
try {
response = httpClient.execute(httpRequestBase);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.println("请求地址:" + httpRequestBase.getURI() + ", 请求方法:" + httpRequestBase.getMethod()
+ ",STATUS CODE = " + response.getStatusLine().getStatusCode());
if (httpRequestBase != null) {
httpRequestBase.abort();
}
throw new Exception("Response Status Code : " + response.getStatusLine().getStatusCode());
} else {
HttpEntity entity = response.getEntity();
if (entity != null) {
entiStr = EntityUtils.toString(entity, Consts.UTF_8);
} else {
throw new Exception("Response Entity Is Null");
}
}
} catch (Exception e) {
throw e;
}
return entiStr;
}
示例7: confirmSingleForQueue
import org.apache.http.Consts; //导入依赖的package包/类
public static String confirmSingleForQueue(Ticket ticket, TrainQuery query) {
CloseableHttpClient httpClient = buildHttpClient();
HttpPost httpPost = new HttpPost(UrlConfig.confirmSingleForQueue);
httpPost.addHeader(CookieManager.cookieHeader());
httpPost.setEntity(new StringEntity(confirmSingleForQueueParam(ticket, query), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)));
String result = StringUtils.EMPTY;
try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
CookieManager.touch(response);
result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
logger.error("confirmSingleForQueue error", e);
}
return result;
}
示例8: post
import org.apache.http.Consts; //导入依赖的package包/类
/**
* post 请求
*
* @param url
* @param xml
* @return
*/
private static String post(String url, String xml) {
try {
HttpEntity entity = Request.Post(url).bodyString(xml, ContentType.create("text/xml", Consts.UTF_8.name())).execute().returnResponse().getEntity();
if (entity != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
entity.writeTo(byteArrayOutputStream);
return byteArrayOutputStream.toString(Consts.UTF_8.name());
}
return null;
} catch (Exception e) {
logger.error("post请求异常," + e.getMessage() + "\npost url:" + url);
e.printStackTrace();
}
return null;
}
示例9: postHttp
import org.apache.http.Consts; //导入依赖的package包/类
public String postHttp(String url, List<NameValuePair> params, List<NameValuePair> headers) throws IOException
{
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
post.getEntity().toString();
if (headers != null)
{
for (NameValuePair header : headers)
{
post.addHeader(header.getName(), header.getValue());
}
}
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null)
{
return EntityUtils.toString(entity);
}
return null;
}
示例10: makeRequest
import org.apache.http.Consts; //导入依赖的package包/类
private String makeRequest(String question) {
try {
HttpPost httpPost = new HttpPost(URL);
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("query", question));
// params.add(new BasicNameValuePair("lang", "it"));
params.add(new BasicNameValuePair("kb", "dbpedia"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
// Error Scenario
if(response.getStatusLine().getStatusCode() >= 400) {
logger.error("QANARY Server could not answer due to: " + response.getStatusLine());
return null;
}
return EntityUtils.toString(response.getEntity());
}
catch(Exception e) {
logger.error(e.getMessage());
}
return null;
}
示例11: parse
import org.apache.http.Consts; //导入依赖的package包/类
/**
* Returns a list of {@link NameValuePair NameValuePairs} as parsed from an
* {@link HttpEntity}. The encoding is taken from the entity's
* Content-Encoding header.
* <p>
* This is typically used while parsing an HTTP POST.
*
* @param entity
* The entity to parse
* @throws IOException
* If there was an exception getting the entity's data.
*/
public static List <NameValuePair> parse (
final HttpEntity entity) throws IOException {
ContentType contentType = ContentType.get(entity);
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
String content = EntityUtils.toString(entity, Consts.ASCII);
if (content != null && content.length() > 0) {
Charset charset = contentType.getCharset();
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
return parse(content, charset);
}
}
return Collections.emptyList();
}
示例12: buildRequestEntity
import org.apache.http.Consts; //导入依赖的package包/类
/**
* Builds a new StringEntity object that's used as HTTP request body.
* Content type of the request is set according to the given headers. If the
* given headers do not contain Content-Type header, "application/xml" is
* used. If the given request body is null or empty, null is returned.
*
* @param requestBody request body
* @param headers HTTP headers to be added to the request
* @return new StringEntity object or null
*/
protected StringEntity buildRequestEntity(String requestBody, Map<String, String> headers) {
String contentTypeHeader = "Content-Type";
LOGGER.debug("Build new request entity.");
// If request body is not null or empty
if (requestBody != null && !requestBody.isEmpty()) {
LOGGER.debug("Request body found.");
// Set content type of the request, default is "application/xml"
String reqContentType = "application/xml";
if (headers != null && !headers.isEmpty()) {
if (headers.get(contentTypeHeader) != null && !headers.get(contentTypeHeader).isEmpty()) {
reqContentType = headers.get(contentTypeHeader);
} else {
LOGGER.warn("\"Content-Type\" header is missing. Use \"application/xml\" as default.");
// No value set, use default value
headers.put(contentTypeHeader, reqContentType);
}
}
// Create request entity that's used as request body
return new StringEntity(requestBody, ContentType.create(reqContentType, Consts.UTF_8));
}
LOGGER.debug("No request body found for request. Null is returned");
return null;
}
示例13: post
import org.apache.http.Consts; //导入依赖的package包/类
/**
* 向目标url发送post请求
*
* @author sheefee
* @date 2017年9月12日 下午5:10:36
* @param url
* @param params
* @return boolean
*/
public static boolean post(String url, Map<String, String> params) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 参数处理
if (params != null && !params.isEmpty()) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> it = params.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(list, Consts.UTF_8));
}
// 执行请求
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
response.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
示例14: post
import org.apache.http.Consts; //导入依赖的package包/类
public String post(String url, String jsonIn) throws IOException, InvalidHttpResponseStatusException {
HttpPost request = new HttpPost(url);
request.setHeader("User-Agent", getUserAgent());
request.setHeader("Content-Type", CONTENT_TYPE);
if (getCredentials() != null) {
request.setHeader("Authorization", getCredentials());
}
if (StringUtils.isBlank(jsonIn)) {
return service(request);
}
StringEntity stringEntity = new StringEntity(jsonIn, Consts.UTF_8);
stringEntity.setContentType(CONTENT_TYPE);
request.setEntity(stringEntity);
return service(request);
}
示例15: testSerialization
import org.apache.http.Consts; //导入依赖的package包/类
@Test
public void testSerialization() throws Exception {
final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "test realm=\"test\", blah=blah, yada=\"yada yada\"");
final TestAuthScheme testScheme = new TestAuthScheme(Consts.ISO_8859_1);
testScheme.processChallenge(challenge);
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(testScheme);
out.flush();
final byte[] raw = buffer.toByteArray();
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw));
final TestAuthScheme authScheme = (TestAuthScheme) in.readObject();
Assert.assertEquals(Consts.ISO_8859_1, authScheme.getCredentialsCharset());
Assert.assertEquals("test", authScheme.getParameter("realm"));
Assert.assertEquals("blah", authScheme.getParameter("blah"));
Assert.assertEquals("yada yada", authScheme.getParameter("yada"));
}