本文整理汇总了Java中org.apache.http.client.methods.HttpPut类的典型用法代码示例。如果您正苦于以下问题:Java HttpPut类的具体用法?Java HttpPut怎么用?Java HttpPut使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpPut类属于org.apache.http.client.methods包,在下文中一共展示了HttpPut类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendHttpPut
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
@Override
public <REQ> CloseableHttpResponse sendHttpPut(String url, REQ request) {
CloseableHttpResponse execute = null;
String requestJson = GsonUtils.toJson(request);
try {
LOGGER.log(Level.FINER, "Send PUT request:" + requestJson + " to url-" + url);
HttpPut httpPut = new HttpPut(url);
StringEntity entity = new StringEntity(requestJson, "UTF-8");
entity.setContentType("application/json");
httpPut.setEntity(entity);
execute = this.httpClientFactory.getHttpClient().execute(httpPut);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Was unable to send PUT request:" + requestJson
+ " (displaying first 1000 chars) from url-" + url, e);
}
return execute;
}
示例2: setDefaultUser
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
public static void setDefaultUser(String usr,String restServerName) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
String body = "{\"default-user\": \""+usr+"\"}";
HttpPut put = new HttpPut("http://"+host+":8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default");
put.addHeader("Content-type", "application/json");
put.setEntity(new StringEntity(body));
HttpResponse response2 = client.execute(put);
HttpEntity respEntity = response2.getEntity();
if(respEntity != null){
String content = EntityUtils.toString(respEntity);
System.out.println(content);
}
}
示例3: process
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
if (authState.getAuthScheme() != null || authState.hasAuthOptions()) {
return;
}
// If no authState has been established and this is a PUT or POST request, add preemptive authorisation
String requestMethod = request.getRequestLine().getMethod();
if (alwaysSendAuth || requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
Credentials credentials = credentialsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (credentials == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.update(authScheme, credentials);
}
}
示例4: getRequest
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
private HttpRequestBase getRequest(String url){
switch(method){
case DELETE:
return new HttpDelete(url);
case GET:
return new HttpGet(url);
case HEAD:
return new HttpHead(url);
case PATCH:
return new HttpPatch(url);
case POST:
return new HttpPost(url);
case PUT:
return new HttpPut(url);
default:
throw new IllegalArgumentException("Invalid or null HttpMethod: " + method);
}
}
示例5: createApacheRequest
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
private HttpRequestBase createApacheRequest(SdkHttpFullRequest request, String uri) {
switch (request.method()) {
case HEAD:
return new HttpHead(uri);
case GET:
return new HttpGet(uri);
case DELETE:
return new HttpDelete(uri);
case OPTIONS:
return new HttpOptions(uri);
case PATCH:
return wrapEntity(request, new HttpPatch(uri));
case POST:
return wrapEntity(request, new HttpPost(uri));
case PUT:
return wrapEntity(request, new HttpPut(uri));
default:
throw new RuntimeException("Unknown HTTP method name: " + request.method());
}
}
示例6: updateModule
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
/**
* Updates the module
*
* @param moduleName
* the name of the module to add
* @param objectToUpdate
* JSON containing the key/value pais to use for update
* @return request object to check status codes and return values
*/
public Response updateModule( String moduleName, JSONObject objectToUpdate )
{
HttpPut request = new HttpPut( this.yambasBase + "modules/" + moduleName );
setAuthorizationHeader( request );
request.addHeader( "x-apiomat-system", this.system.toString( ) );
request.setEntity( new StringEntity( objectToUpdate.toString( ), ContentType.APPLICATION_JSON ) );
try
{
final HttpResponse response = this.client.execute( request );
return new Response( response );
}
catch ( final IOException e )
{
e.printStackTrace( );
}
return null;
}
示例7: doPut
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
/**
* Put String
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
return httpClient.execute(request);
}
示例8: createPutRequest
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
/**
*
* @param payload
* @return
*/
protected HttpPut createPutRequest(final Object value, final String collection) {
try {
logger.debug("received value {}, and collection {}", value, collection);
final Map<?, ?> valueMap = new LinkedHashMap<>((Map<?,?>)value);
final Object url = valueMap.remove(URL);
final URIBuilder uriBuilder = getURIBuilder(null == url ? UUID.randomUUID().toString() : url.toString(), collection);
final String jsonString = MAPPER.writeValueAsString(valueMap);
final HttpPut request = new HttpPut(uriBuilder.build());
final StringEntity params = new StringEntity(jsonString, "UTF-8");
params.setContentType(DEFAULT_CONTENT_TYPE.toString());
request.setEntity(params);
return request;
} catch (URISyntaxException | JsonProcessingException | MalformedURLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
示例9: createHttpRequest
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
switch(method.toUpperCase(Locale.ROOT)) {
case HttpDeleteWithEntity.METHOD_NAME:
return addRequestBody(new HttpDeleteWithEntity(uri), entity);
case HttpGetWithEntity.METHOD_NAME:
return addRequestBody(new HttpGetWithEntity(uri), entity);
case HttpHead.METHOD_NAME:
return addRequestBody(new HttpHead(uri), entity);
case HttpOptions.METHOD_NAME:
return addRequestBody(new HttpOptions(uri), entity);
case HttpPatch.METHOD_NAME:
return addRequestBody(new HttpPatch(uri), entity);
case HttpPost.METHOD_NAME:
HttpPost httpPost = new HttpPost(uri);
addRequestBody(httpPost, entity);
return httpPost;
case HttpPut.METHOD_NAME:
return addRequestBody(new HttpPut(uri), entity);
case HttpTrace.METHOD_NAME:
return addRequestBody(new HttpTrace(uri), entity);
default:
throw new UnsupportedOperationException("http method not supported: " + method);
}
}
示例10: sendPutCommand
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
/**
* sendPutCommand
*
* @param url
* @param parameters
* @return
* @throws ClientProtocolException
*/
public Map<String, Object> sendPutCommand(String url, Map<String, Object> credentials,
Map<String, String> parameters) throws ManagerResponseException {
Map<String, Object> response = new HashMap<String, Object>();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-type", "application/json");
try {
ObjectMapper mapper = new ObjectMapper();
StringEntity entity = new StringEntity(mapper.writeValueAsString(parameters));
httpPut.setEntity(entity);
CloseableHttpResponse httpResponse = httpclient.execute(httpPut, localContext);
ResponseHandler<String> handler = new CustomResponseErrorHandler();
String body = handler.handleResponse(httpResponse);
response.put(BODY, body);
httpResponse.close();
} catch (Exception e) {
throw new ManagerResponseException(e.getMessage(), e);
}
return response;
}
示例11: writeA
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
public boolean writeA(String key,String[] all_data) {
try {
StringBuffer sb = new StringBuffer();
sb.append("{"+QUATA+key+QUATA+":[");
for (String i : all_data) {
sb.append(QUATA+i+QUATA+",");
}
String data = sb.toString();
data = data.substring(0, data.length() - 1);
String node = data + "]}";
HttpClient httpclient = HttpClients.createDefault();
HttpPut httppost = new HttpPut(getChannelUrl()); // Use PUT prevents key generation for each as a parent
StringEntity entity = new StringEntity(node);
httppost.setEntity(entity);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
this.resetChannel();
return response.getStatusLine().getStatusCode() == 200;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
示例12: doPut
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
/**
* Put stream
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
return httpClient.execute(request);
}
示例13: put
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
private Map put(String url, String data) throws IOException, HttpException {
Map<String,Object> map = null;
CredentialsProvider credentials = credentialsProvider();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credentials)
.build();
try {
HttpPut httpPut = new HttpPut(url);
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-Type", "application/json");
HttpEntity entity = new ByteArrayEntity(data.getBytes("utf-8"));
httpPut.setEntity(entity);
System.out.println("Executing request " + httpPut.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpPut);
try {
LOG.debug("----------------------------------------");
LOG.debug((String)response.getStatusLine().getReasonPhrase());
String responseBody = EntityUtils.toString(response.getEntity());
LOG.debug(responseBody);
Gson gson = new Gson();
map = new HashMap<String,Object>();
map = (Map<String,Object>) gson.fromJson(responseBody, map.getClass());
LOG.debug(responseBody);
} finally {
response.close();
}
} finally {
httpclient.close();
}
return map;
}
开发者ID:dellemc-symphony,项目名称:ticketing-service-paqx-parent-sample,代码行数:35,代码来源:TicketingIntegrationService.java
示例14: sendPut
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
private JsonObject sendPut(String domain, URI target, JsonObject payload) {
String authObj = getAuthObj(domain, "PUT", target, payload);
String sign = global.getSignMgr().sign(authObj);
String key = "ed25519:" + global.getKeyMgr().getCurrentIndex();
HttpPut req = new HttpPut(target);
req.setEntity(getJsonEntity(payload));
req.setHeader("Host", domain);
req.setHeader("Authorization",
"X-Matrix origin=" + global.getDomain() + ",key=\"" + key + "\",sig=\"" + sign + "\"");
log.info("Calling [{}] {}", domain, req);
try (CloseableHttpResponse res = client.execute(req)) {
int resStatus = res.getStatusLine().getStatusCode();
JsonObject body = getBody(res.getEntity());
if (resStatus == 200) {
log.info("Got answer");
return body;
} else {
throw new FederationException(resStatus, body);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例15: doPut
import org.apache.http.client.methods.HttpPut; //导入依赖的package包/类
/**
* Put String
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
return httpClient.execute(request);
}