本文整理了Java中com.google.appengine.api.urlfetch.HTTPRequest
类的一些代码示例,展示了HTTPRequest
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HTTPRequest
类的具体详情如下:
包路径:com.google.appengine.api.urlfetch.HTTPRequest
类名称:HTTPRequest
暂无
代码示例来源:origin: googlemaps/google-maps-services-java
@Override
public <T, R extends ApiResponse<T>> PendingResult<T> handlePost(
String hostName,
String url,
String payload,
String userAgent,
Class<R> clazz,
FieldNamingPolicy fieldNamingPolicy,
long errorTimeout,
Integer maxRetries,
ExceptionsAllowedToRetry exceptionsAllowedToRetry) {
FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10);
HTTPRequest req = null;
try {
req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions);
req.setHeader(new HTTPHeader("Content-Type", "application/json; charset=utf-8"));
req.setPayload(payload.getBytes(UTF_8));
} catch (MalformedURLException e) {
LOG.error("Request: {}{}", hostName, url, e);
throw (new RuntimeException(e));
}
return new GaePendingResult<>(
req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry);
}
代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client
static HTTPRequest copyRequest(HTTPRequest in) {
HTTPRequest out = new HTTPRequest(in.getURL(), in.getMethod(), in.getFetchOptions());
for (HTTPHeader h : in.getHeaders()) {
out.addHeader(h);
}
out.setPayload(in.getPayload());
return out;
}
}
代码示例来源:origin: stackoverflow.com
httpRequest.addHeader(new HTTPHeader("Content-Type","application/json"));
httpRequest.addHeader(new HTTPHeader("Authorization", "key=" + API_KEY));
httpRequest.setPayload(jsonString.getBytes("UTF-8"));
LOG.info("Sending POST request to: " + GCM_URL);
response = URLFetchServiceFactory.getURLFetchService().fetch(httpRequest);
List<HTTPHeader> hdrs = response.getHeaders();
for(HTTPHeader header : hdrs) {
LOG.info("Header: " + header.getName());
LOG.info("Value: " + header.getValue());
代码示例来源:origin: GoogleCloudPlatform/appengine-tck
@Test
public void testHeaders() throws Exception {
URLFetchService service = URLFetchServiceFactory.getURLFetchService();
URL url = getFetchUrl();
HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
req.setPayload("Headers!".getBytes(UTF_8));
HTTPResponse response = service.fetch(req);
boolean found = false;
List<HTTPHeader> headers = response.getHeadersUncombined();
for (HTTPHeader h : headers) {
if (h.getName().equals("ABC")) {
Assert.assertEquals("123", h.getValue());
found = true;
break;
}
}
Assert.assertTrue("Cannot find matching header <ABC : 123>: " + headers, found);
found = false;
headers = response.getHeaders();
for (HTTPHeader h : headers) {
if (h.getName().equals("XYZ")) {
Assert.assertEquals("1, 2, 3", h.getValue());
found = true;
break;
}
}
Assert.assertTrue("Cannot find matching header <XYZ : 1,2,3>: " + headers, found);
}
代码示例来源:origin: stackoverflow.com
URL fetchurl = new URL(url);
String nameAndPassword = credentials.get("name")+":"+credentials.get("password");
String authorizationString = "Basic " + Base64.encode(nameAndPassword.getBytes());
HTTPRequest request = new HTTPRequest(fetchurl);
request.addHeader(new HTTPHeader("Authorization", authorizationString));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
System.out.println(new String(response.getContent()));
代码示例来源:origin: jclouds/legacy-jclouds
@Test
void testConvertRequestNoContent() throws IOException {
HttpRequest request = HttpRequest.builder().method(HttpMethod.GET).endpoint(endPoint).build();
HTTPRequest gaeRequest = req.apply(request);
assert gaeRequest.getPayload() == null;
assertEquals(gaeRequest.getHeaders().size(), 1);// user agent
assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.USER_AGENT);
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "jclouds/1.0 urlfetch/1.4.3");
}
代码示例来源:origin: com.google.http-client/google-http-client-appengine
@Override
public void addHeader(String name, String value) {
request.addHeader(new HTTPHeader(name, value));
}
代码示例来源:origin: stackoverflow.com
String json ="{}";
URL url = new URL("https://android.googleapis.com/gcm/send");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.addHeader(new HTTPHeader("Content-Type","application/json"));
request.addHeader(new HTTPHeader("Authorization", "key=<>"));
request.setPayload(json.getBytes("UTF-8"));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
代码示例来源:origin: com.force.api/force-wsc
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, fetchOptions);
request.addHeader(new HTTPHeader("User-Agent", VersionInfo.info()));
request.addHeader(new HTTPHeader(ent.getKey(), ent.getValue()));
request.addHeader(new HTTPHeader("Content-Encoding", "gzip"));
request.addHeader(new HTTPHeader("Accept-Encoding", "gzip"));
+ config.getProxyPassword();
String auth = "Basic " + new String(Base64.encode(token.getBytes()));
request.addHeader(new HTTPHeader("Proxy-Authorization", auth));
request.addHeader(new HTTPHeader("Https-Proxy-Authorization", auth));
request.addHeader(new HTTPHeader(entry.getKey(), entry.getValue()));
config.getTraceStream().println("WSC: Request configured to have headers " + request.getHeaders());
代码示例来源:origin: googlemaps/google-maps-services-java
@Override
public <T, R extends ApiResponse<T>> PendingResult<T> handle(
String hostName,
String url,
String userAgent,
Class<R> clazz,
FieldNamingPolicy fieldNamingPolicy,
long errorTimeout,
Integer maxRetries,
ExceptionsAllowedToRetry exceptionsAllowedToRetry) {
FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10);
HTTPRequest req;
try {
req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions);
} catch (MalformedURLException e) {
LOG.error("Request: {}{}", hostName, url, e);
throw (new RuntimeException(e));
}
return new GaePendingResult<>(
req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry);
}
代码示例来源:origin: com.threewks.thundr/thundr-gae
private void setContentTypeIfNotPresent(HttpRequestImpl request, HTTPRequest fetchRequest, ContentType contentType) {
if (!request.getHeaders().containsKey(Header.ContentType)) {
fetchRequest.setHeader(new HTTPHeader(Header.ContentType, contentType.value()));
}
}
代码示例来源:origin: jclouds/legacy-jclouds
@Test
void testConvertRequestSetsHeaders() throws IOException {
HttpRequest request = HttpRequest.builder()
.method(HttpMethod.GET)
.endpoint(endPoint)
.addHeader("foo", "bar").build();
HTTPRequest gaeRequest = req.apply(request);
assertEquals(gaeRequest.getHeaders().get(0).getName(), "foo");
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "bar");
}
代码示例来源:origin: com.squareup.retrofit/retrofit
static Response parseResponse(HTTPResponse response, HTTPRequest creatingRequest) {
// Response URL will be null if it is the same as the request URL.
URL responseUrl = response.getFinalUrl();
String urlString = (responseUrl != null ? responseUrl : creatingRequest.getURL()).toString();
int status = response.getResponseCode();
List<HTTPHeader> fetchHeaders = response.getHeaders();
List<Header> headers = new ArrayList<Header>(fetchHeaders.size());
String contentType = "application/octet-stream";
for (HTTPHeader fetchHeader : fetchHeaders) {
String name = fetchHeader.getName();
String value = fetchHeader.getValue();
if ("Content-Type".equalsIgnoreCase(name)) {
contentType = value;
}
headers.add(new Header(name, value));
}
TypedByteArray body = null;
byte[] fetchBody = response.getContent();
if (fetchBody != null) {
body = new TypedByteArray(contentType, fetchBody);
}
return new Response(urlString, status, "", headers, body);
}
}
代码示例来源:origin: com.threewks.thundr/thundr-gae
private void addBody(HttpRequestImpl request, HTTPRequest fetchRequest, boolean createFormPostIfBodyIsEmpty) {
byte[] data = null;
try {
Object body = request.getBody();
if (body == null && createFormPostIfBodyIsEmpty) {
body = createFormPostBody(request);
}
if (isMultipart(request)) {
HttpEntity multipart = prepareMultipart(request);
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
multipart.writeTo(baos);
data = baos.toByteArray();
fetchRequest.setHeader(new HTTPHeader(Header.ContentType, multipart.getContentType().getValue()));
} else if (body != null) {
InputStream is = convertOutgoing(body);
data = Streams.readBytes(is);
}
} catch (Exception e) {
throw new HttpException(e, "Failed to generate request body: %s", e.getMessage());
}
if (data != null) {
fetchRequest.setPayload(data);
}
}
代码示例来源:origin: org.apache.camel/camel-gae
protected void writeRequestBody(GHttpEndpoint endpoint, Exchange exchange, HTTPRequest request) {
request.setPayload(exchange.getIn().getBody(byte[].class));
}
代码示例来源:origin: com.dropbox.core/dropbox-core-sdk
@Override
public Response finish() throws IOException {
if (request == null) {
throw new IllegalStateException("Uploader already closed.");
}
request.setPayload(body.toByteArray());
HTTPResponse response = service.fetch(request);
Response requestorResponse = toRequestorResponse(response);
close();
if (progressListener != null) {
progressListener.onProgress(request.getPayload().length);
}
return requestorResponse;
}
}
代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client
HTTPRequestInfo(HTTPRequest req) {
method = req.getMethod().toString();
byte[] payload = req.getPayload();
length = payload == null ? -1 : payload.length;
url = req.getURL();
h1 = null;
h2 = req.getHeaders();
}
代码示例来源:origin: stackoverflow.com
URL url = new URL(request);
HTTPRequest req = new HTTPRequest(url, HTTPMethod.GET);
req.addHeader(new HTTPHeader("Authorization", "Bearer " + bearerToken));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(req);
System.out.println(new String(response.getContent()));
代码示例来源:origin: com.google.http-client/google-http-client-extensions
@Override
public void setTimeout(int connectTimeout, int readTimeout) {
request.getFetchOptions().setDeadline(
connectTimeout == 0 || readTimeout == 0 ? Double.MAX_VALUE : (connectTimeout + readTimeout)
/ 1000.0);
}
代码示例来源:origin: com.threewks.thundr/thundr-gae
protected HttpResponseImpl fetch(HTTPRequest request, HTTPMethod headGetDelete) {
try {
Future<HTTPResponse> fetchAsync = fetchService.fetchAsync(request);
return new HttpResponseImpl(fetchAsync, this, request.getURL());
} catch (Exception e) {
throw new HttpRequestException(e, "Failed to create a %s request: %s", headGetDelete, e.getMessage());
}
}
内容来源于网络,如有侵权,请联系作者删除!