use of org.apache.http.client.methods.HttpHead in project aliyun-oss-java-sdk by aliyun.
the class HttpRequestFactory method createHttpRequest.
public HttpRequestBase createHttpRequest(ServiceClient.Request request, ExecutionContext context) {
String uri = request.getUri();
HttpRequestBase httpRequest;
HttpMethod method = request.getMethod();
if (method == HttpMethod.POST) {
HttpPost postMethod = new HttpPost(uri);
if (request.getContent() != null) {
postMethod.setEntity(new RepeatableInputStreamEntity(request));
}
httpRequest = postMethod;
} else if (method == HttpMethod.PUT) {
HttpPut putMethod = new HttpPut(uri);
if (request.getContent() != null) {
if (request.isUseChunkEncoding()) {
putMethod.setEntity(buildChunkedInputStreamEntity(request));
} else {
putMethod.setEntity(new RepeatableInputStreamEntity(request));
}
}
httpRequest = putMethod;
} else if (method == HttpMethod.GET) {
httpRequest = new HttpGet(uri);
} else if (method == HttpMethod.DELETE) {
httpRequest = new HttpDelete(uri);
} else if (method == HttpMethod.HEAD) {
httpRequest = new HttpHead(uri);
} else if (method == HttpMethod.OPTIONS) {
httpRequest = new HttpOptions(uri);
} else {
throw new ClientException("Unknown HTTP method name: " + method.toString());
}
configureRequestHeaders(request, context, httpRequest);
return httpRequest;
}
use of org.apache.http.client.methods.HttpHead in project aliyun-oss-java-sdk by aliyun.
the class HttpFactoryTest method testCreateHttpRequest.
@Test
public void testCreateHttpRequest() throws Exception {
ExecutionContext context = new ExecutionContext();
String url = "http://127.0.0.1";
String content = "This is a test request";
byte[] contentBytes = null;
try {
contentBytes = content.getBytes(context.getCharset());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
fail(e.getMessage());
}
HttpRequestFactory factory = new HttpRequestFactory();
ServiceClient.Request request = new ServiceClient.Request();
request.setUrl(url);
HttpRequestBase httpRequest = null;
// GET
request.setMethod(HttpMethod.GET);
httpRequest = factory.createHttpRequest(request, context);
HttpGet getMethod = (HttpGet) httpRequest;
assertEquals(url, getMethod.getURI().toString());
// DELETE
request.setMethod(HttpMethod.DELETE);
httpRequest = factory.createHttpRequest(request, context);
HttpDelete delMethod = (HttpDelete) httpRequest;
assertEquals(url, delMethod.getURI().toString());
// HEAD
request.setMethod(HttpMethod.HEAD);
httpRequest = factory.createHttpRequest(request, context);
HttpHead headMethod = (HttpHead) httpRequest;
assertEquals(url, headMethod.getURI().toString());
// POST
request.setContent(new ByteArrayInputStream(contentBytes));
request.setContentLength(contentBytes.length);
request.setMethod(HttpMethod.POST);
httpRequest = factory.createHttpRequest(request, context);
HttpPost postMethod = (HttpPost) httpRequest;
assertEquals(url, postMethod.getURI().toString());
HttpEntity entity = postMethod.getEntity();
try {
assertEquals(content, readSting(entity.getContent()));
} catch (IllegalStateException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
// PUT
request.setContent(new ByteArrayInputStream(contentBytes));
request.setContentLength(contentBytes.length);
request.setMethod(HttpMethod.PUT);
httpRequest = factory.createHttpRequest(request, context);
HttpPut putMethod = (HttpPut) httpRequest;
assertEquals(url, putMethod.getURI().toString());
entity = putMethod.getEntity();
try {
assertEquals(content, readSting(entity.getContent()));
} catch (IllegalStateException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
try {
request.close();
} catch (IOException e) {
}
}
use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.
the class RestConnection method createHead.
public HttpHead createHead(URI uri, String accept, boolean cookie) {
HttpHead head = new HttpHead(uri);
decorateHttpMessage(head, accept, "en", cookie);
return head;
}
use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.
the class WebDAVConnection method head.
public HttpResponse head(URI uri) throws IOException, URISyntaxException {
HttpHead propfind = new HttpHead(uri);
HttpResponse response = execute(propfind);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
return response;
}
use of org.apache.http.client.methods.HttpHead in project ais-sdk by huaweicloudsdk.
the class AccessServiceImpl method createRequest.
/**
* Make a request that can be sent by the HTTP client.
*
* @param url
* specifies the API access path.
* @param header
* specifies the header information to be added.
* @param content
* specifies the body content to be sent in the API call.
* @param contentLength
* specifies the length of the content. This parameter is optional.
* @param httpMethod
* specifies the HTTP method to be used.
* @return specifies the request that can be sent by an HTTP client.
*/
private static HttpRequestBase createRequest(URL url, Header header, InputStream content, Long contentLength, HttpMethodName httpMethod) {
HttpRequestBase httpRequest;
if (httpMethod == HttpMethodName.POST) {
HttpPost postMethod = new HttpPost(url.toString());
if (content != null) {
InputStreamEntity entity = new InputStreamEntity(content, contentLength);
postMethod.setEntity(entity);
}
httpRequest = postMethod;
} else if (httpMethod == HttpMethodName.PUT) {
HttpPut putMethod = new HttpPut(url.toString());
httpRequest = putMethod;
if (content != null) {
InputStreamEntity entity = new InputStreamEntity(content, contentLength);
putMethod.setEntity(entity);
}
} else if (httpMethod == HttpMethodName.PATCH) {
HttpPatch patchMethod = new HttpPatch(url.toString());
httpRequest = patchMethod;
if (content != null) {
InputStreamEntity entity = new InputStreamEntity(content, contentLength);
patchMethod.setEntity(entity);
}
} else if (httpMethod == HttpMethodName.GET) {
httpRequest = new HttpGet(url.toString());
} else if (httpMethod == HttpMethodName.DELETE) {
httpRequest = new HttpDelete(url.toString());
} else if (httpMethod == HttpMethodName.HEAD) {
httpRequest = new HttpHead(url.toString());
} else {
throw new RuntimeException("Unknown HTTP method name: " + httpMethod);
}
httpRequest.addHeader(header);
return httpRequest;
}
Aggregations