本文整理匯總了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);
}
}