本文整理汇总了Java中org.apache.http.client.methods.HttpPost.setConfig方法的典型用法代码示例。如果您正苦于以下问题:Java HttpPost.setConfig方法的具体用法?Java HttpPost.setConfig怎么用?Java HttpPost.setConfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.HttpPost
的用法示例。
在下文中一共展示了HttpPost.setConfig方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: simplePost
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
/**
* Simple Http Post.
*
* @param path the path
* @param payload the payload
* @return the closeable http response
* @throws URISyntaxException the URI syntax exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws MininetException the MininetException
*/
public CloseableHttpResponse simplePost(String path, String payload)
throws URISyntaxException, IOException, MininetException {
URI uri = new URIBuilder()
.setScheme("http")
.setHost(mininetServerIP.toString())
.setPort(mininetServerPort.getPort())
.setPath(path)
.build();
CloseableHttpClient client = HttpClientBuilder.create().build();
RequestConfig config = RequestConfig
.custom()
.setConnectTimeout(CONNECTION_TIMEOUT_MS)
.setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
.setSocketTimeout(CONNECTION_TIMEOUT_MS)
.build();
HttpPost request = new HttpPost(uri);
request.setConfig(config);
request.addHeader("Content-Type", "application/json");
request.setEntity(new StringEntity(payload));
CloseableHttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() >= 300) {
throw new MininetException(String.format("failure - received a %d for %s.",
response.getStatusLine().getStatusCode(), request.getURI().toString()));
}
return response;
}
示例2: execute
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
@Override
public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
}finally {
httpPost.releaseConnection();
}
}
示例3: testPost
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
@Test
public void testPost() throws IOException {
String ip = "冰箱冰箱冰箱冰箱冰箱冰箱冰箱";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
// 请求参数
StringEntity entity = new StringEntity("", DEFAULT_ENCODE);
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
HttpPost httpPost = new HttpPost("https://m.fangliaoyun.com");
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
//此处区别PC终端类型
httpPost.addHeader("typeFlg", "9");
//此处增加浏览器端访问IP
httpPost.addHeader("x-forwarded-for", ip);
httpPost.addHeader("Proxy-Client-IP", ip);
httpPost.addHeader("WL-Proxy-Client-IP", ip);
httpPost.addHeader("HTTP_CLIENT_IP", ip);
httpPost.addHeader("X-Real-IP", ip);
httpPost.addHeader("Host", ip);
httpPost.setEntity(entity);
httpPost.setConfig(RequestConfig.DEFAULT);
HttpResponse httpResponse;
// post请求
httpResponse = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println(httpEntity.getContent());
//释放资源
closeableHttpClient.close();
}
示例4: execute
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
@Override
public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}finally {
httpPost.releaseConnection();
}
}
示例5: execute
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
@Override
public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
}finally {
httpPost.releaseConnection();
}
}
示例6: apiRequest
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
private HttpRequestBase apiRequest(String apiMethod, List<NameValuePair> params) {
HttpPost post = new HttpPost(getRestApiUrl(apiMethod));
post.setConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build());
List<NameValuePair> formFields = new ArrayList<>();
formFields.add(new BasicNameValuePair("api.token", myPassword));
formFields.addAll(params);
try {
post.setEntity(new UrlEncodedFormEntity(formFields, "UTF-8"));
}
catch (UnsupportedEncodingException ignored) {
// cannot happen
}
return post;
}
示例7: doPost
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
public String doPost(String url,List <NameValuePair> nvps,BasicCookieStore loginStatus) throws Exception{
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(loginStatus).build();
String webStr = "";
try {
//1Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);//设置超时配置
/*
* nvps 内容
* List <NameValuePair> nvps = new ArrayList <NameValuePair>();
* //网页post参数 http://www.scnj.tv/appqy_api/api.php?api_key=AaPpQqYyCcOoMmLl&opt=getCategory
* nvps.add(new BasicNameValuePair("api_key", WEB_KEY));
* nvps.add(new BasicNameValuePair("opt", "login"));
* nvps.add(new BasicNameValuePair("uid", uid));
* nvps.add(new BasicNameValuePair("pass", password));
* */
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));//需加上UTF8 不然提交出去到网站会变成乱码 TODO 以后需要提取配置编码
CloseableHttpResponse response2;
//2发送请求
response2 = httpclient.execute(httpPost);
System.out.println(response2.getStatusLine());
//3处理响应结果
if (response2.getStatusLine().getStatusCode() == 200) {
//4从输入流读取网页字符串内容
HttpEntity entity2 = response2.getEntity();
InputStream in = entity2.getContent();
webStr = readResponse(in);
//log.debug("网络请求接收到的返回数据"+webStr);
EntityUtils.consume(entity2);
}
// and ensure it is fully consumed
response2.close();
}catch(java.io.IOException e){
e.getStackTrace();
log.error("Web服务器端网络异常!info:"+e.getStackTrace().toString());
}
finally {
//始终保持执行
httpclient.close();
}
return webStr;
}
示例8: execute
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
@Override
public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = httpclient.execute(httpPost);
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);){
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
}finally {
httpPost.releaseConnection();
}
}
示例9: execute
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
@Override
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(response);
}
if (material == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build());
}
File file = material.getFile();
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
}
httpPost.setEntity(multipartEntityBuilder.build());
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
} finally {
httpPost.releaseConnection();
}
}
示例10: requestHttpPost
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
public String requestHttpPost(String url_prex, String url, Map<String, String> params, String authorization)
throws HttpException, IOException {
url = url_prex + url;
HttpPost method = this.httpPostMethod(url, authorization);
String paramsstr = JSON.toJSONString(params);
StringEntity sendEntity = new StringEntity(paramsstr);
method.setEntity(sendEntity);
method.setConfig(requestConfig);
HttpEntity httpEntity = method.getEntity();
for (int i = 0; i < method.getAllHeaders().length; i++) {
Header header = method.getAllHeaders()[i];
}
HttpResponse response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity == null) {
return "";
}
InputStream is = null;
String responseData = "";
try {
is = entity.getContent();
responseData = IOUtils.toString(is, "UTF-8");
} finally {
if (is != null) {
is.close();
}
}
return responseData;
}
示例11: HttpPostDeliverService
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
public HttpPostDeliverService(final String postUrl, final int connectTimeout, final int soTimeout) {
httpClient = HttpAsyncClients.createDefault();
httpClient.start();
httpPost = new HttpPost(postUrl);
final RequestConfig requestConfig =
RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(soTimeout).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Content-Type", "text/html;charset=UTF-8");
}
示例12: executePostRequest
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
private String[] executePostRequest(String url, Map<String, String> parameters) throws IOException {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : parameters.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
HttpPost request = new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(params, StringUtil.ENCODING_UTF8));
request.setConfig(requestConfig);
return executeRequest(request);
}
示例13: post
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
/** post
* @param url 请求的url
* @param queries 请求的参数,在浏览器?后面的数据,没有可以传null
* @param params post form 提交的参数
* @return
* @throws IOException
*/
public String post(String url, Map<String, String> queries, Map<String, String> params) throws IOException {
String responseBody = "";
CloseableHttpClient httpClient = getHttpClient();
StringBuilder sb = new StringBuilder(url);
appendQueryParams(queries, sb);
//指定url,和http方式
HttpPost httpPost = new HttpPost(sb.toString());
if (SetTimeOut) {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(SocketTimeout)
.setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
}
//添加参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null && params.keySet().size() > 0) {
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
nvps.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
//请求数据
CloseableHttpResponse response = httpClient.execute(httpPost);
responseBody = getResponseString(responseBody, response);
return responseBody;
}
示例14: postJson
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
/** post
* @param url 请求的url
* @param queries 请求的参数,在浏览器?后面的数据,没有可以传null
* @param obj post obj 提交json
* @return
* @throws IOException
*/
public <T> String postJson(String url, Map<String, String> queries, T obj) throws IOException {
String responseBody = "";
CloseableHttpClient httpClient = getHttpClient();
StringBuilder sb = new StringBuilder(url);
appendQueryParams(queries, sb);
//指定url,和http方式
HttpPost httpPost = new HttpPost(sb.toString());
if (SetTimeOut) {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(SocketTimeout)
.setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
}
//添加参数
String jsonbody = null;
if (obj != null) {
jsonbody = JSONObject.toJSONString(obj);
}
//设置参数到请求对象中
StringEntity stringEntity = new StringEntity(jsonbody,"utf-8");//解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
//请求数据
CloseableHttpResponse response = httpClient.execute(httpPost);
responseBody = getResponseString(responseBody, response);
return responseBody;
}
示例15: executeWithKey
import org.apache.http.client.methods.HttpPost; //导入方法依赖的package包/类
private String executeWithKey(String url, String requestStr) throws WxErrorException {
try {
SSLContext sslContext = getConfig().getSslContext();
if (null == sslContext) {
throw new IllegalArgumentException("请先初始化配置类(即WxMpConfigStorage的实现类)中的SSLContext!");
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[]{"TLSv1"}, null,
new DefaultHostnameVerifier());
HttpPost httpPost = new HttpPost(url);
if (this.wxMpService.getHttpProxy() != null) {
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
}
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).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;
}
} finally {
httpPost.releaseConnection();
}
} catch (Exception 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);
}
}