当前位置: 首页>>代码示例>>Java>>正文


Java URLFetchService.fetch方法代码示例

本文整理汇总了Java中com.google.appengine.api.urlfetch.URLFetchService.fetch方法的典型用法代码示例。如果您正苦于以下问题:Java URLFetchService.fetch方法的具体用法?Java URLFetchService.fetch怎么用?Java URLFetchService.fetch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.appengine.api.urlfetch.URLFetchService的用法示例。


在下文中一共展示了URLFetchService.fetch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: _doRequest

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
/**
 * Hace la llamada a URLFetch de google
 * @throws IOException
 */
private void _doRequest() throws IOException {
	URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();

	FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
	fetchOptions.doNotValidateCertificate();
	fetchOptions.setDeadline(_conxTimeOut);

	HTTPRequest request = new HTTPRequest(this.getURL(),_requestMethod,
										  fetchOptions);
	if (_requestOS != null) {
		byte[] bytes = _requestOS.toByteArray();
		if (bytes != null && bytes.length > 0) {
			request.setPayload(bytes);
		}
	}
	HTTPResponse httpResponse = urlFetchService.fetch(request);
	_responseCode = httpResponse.getResponseCode();
	_responseIS = new ByteArrayInputStream(httpResponse.getContent());
}
 
开发者ID:opendata-euskadi,项目名称:r01fb,代码行数:24,代码来源:HttpGoogleURLFetchConnectionWrapper.java

示例2: doGet

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
    ServletException {
  String trackingId = System.getenv("GA_TRACKING_ID");
  URIBuilder builder = new URIBuilder();
  builder.setScheme("http").setHost("www.google-analytics.com").setPath("/collect")
      .addParameter("v", "1") // API Version.
      .addParameter("tid", trackingId) // Tracking ID / Property ID.
      // Anonymous Client Identifier. Ideally, this should be a UUID that
      // is associated with particular user, device, or browser instance.
      .addParameter("cid", "555")
      .addParameter("t", "event") // Event hit type.
      .addParameter("ec", "example") // Event category.
      .addParameter("ea", "test action"); // Event action.
  URI uri = null;
  try {
    uri = builder.build();
  } catch (URISyntaxException e) {
    throw new ServletException("Problem building URI", e);
  }
  URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
  URL url = uri.toURL();
  fetcher.fetch(url);
  resp.getWriter().println("Event tracked.");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:26,代码来源:AnalyticsServlet.java

示例3: doGet

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException {
  String url = req.getParameter("url");
  String deadlineSecs = req.getParameter("deadline");
  URLFetchService service = URLFetchServiceFactory.getURLFetchService();
  HTTPRequest fetchReq = new HTTPRequest(new URL(url));
  if (deadlineSecs != null) {
    fetchReq.getFetchOptions().setDeadline(Double.valueOf(deadlineSecs));
  }
  HTTPResponse fetchRes = service.fetch(fetchReq);
  for (HTTPHeader header : fetchRes.getHeaders()) {
    res.addHeader(header.getName(), header.getValue());
  }
  if (fetchRes.getResponseCode() == 200) {
    res.getOutputStream().write(fetchRes.getContent());
  } else {
    res.sendError(fetchRes.getResponseCode(), "Error while fetching");
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-java-vm-runtime,代码行数:20,代码来源:CurlServlet.java

示例4: getResponseDELETE

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
public static String getResponseDELETE(String url, Map<String, String> params, Map<String, String> headers) {
	int retry = 0;
	while (retry < 3) {
		long start = System.currentTimeMillis();
		try {
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			String urlStr = ToolString.toUrl(url.trim(), params);
			logger.debug("DELETE : " + urlStr);
			HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.DELETE, FetchOptions.Builder.withDeadline(deadline));
			HTTPResponse response = fetcher.fetch(httpRequest);
			return processResponse(response);
		} catch (Throwable e) {
			retry++;
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			} else if (retry < 3) {
				logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage());
			} else {
				logger.error(e.getClass() + "\n" + ToolString.stack2string(e));
			}
		}
	}
	return null;
}
 
开发者ID:rafali,项目名称:flickr-uploader,代码行数:25,代码来源:HttpClientGAE.java

示例5: getResponseProxyPOST

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
public static String getResponseProxyPOST(URL url) throws MalformedURLException, UnsupportedEncodingException, IOException {
	URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
	String base64payload = "base64url=" + Base64UrlSafe.encodeServer(url.toString());
	String urlStr = url.toString();
	if (urlStr.contains("?")) {
		base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr.substring(0, urlStr.indexOf("?")));
		base64payload += "&base64content=" + Base64UrlSafe.encodeServer(urlStr.substring(urlStr.indexOf("?") + 1));
	} else {
		base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr);
	}
	HTTPRequest httpRequest = new HTTPRequest(new URL(HttpClientGAE.POSTPROXY_PHP), HTTPMethod.POST, FetchOptions.Builder.withDeadline(30d).doNotValidateCertificate());
	httpRequest.setPayload(base64payload.getBytes(UTF8));
	HTTPResponse response = fetcher.fetch(httpRequest);
	String processResponse = HttpClientGAE.processResponse(response);
	logger.info("proxying " + url + "\nprocessResponse:" + processResponse);
	return processResponse;
}
 
开发者ID:rafali,项目名称:flickr-uploader,代码行数:18,代码来源:HttpClientGAE.java

示例6: makeHttpRequest

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
/**
 * Creates an HTTPRequest with the information passed in.
 *
 * @param accessToken the access token necessary to authorize the request.
 * @param url the url to query.
 * @param payload the payload for the request.
 * @return the created HTTP request.
 * @throws IOException
 */
public static HTTPResponse makeHttpRequest(
    String accessToken, final String url, String payload, HTTPMethod method) throws IOException {

  // Create HTTPRequest and set headers
  HTTPRequest httpRequest = new HTTPRequest(new URL(url.toString()), method);
  httpRequest.addHeader(new HTTPHeader("Authorization", "OAuth " + accessToken));
  httpRequest.addHeader(new HTTPHeader("Host", "www.googleapis.com"));
  httpRequest.addHeader(new HTTPHeader("Content-Length", Integer.toString(payload.length())));
  httpRequest.addHeader(new HTTPHeader("Content-Type", "application/json"));
  httpRequest.addHeader(new HTTPHeader("User-Agent", "google-api-java-client/1.0"));
  httpRequest.setPayload(payload.getBytes());

  URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
  HTTPResponse httpResponse = fetcher.fetch(httpRequest);
  return httpResponse;
}
 
开发者ID:GoogleCloudPlatform,项目名称:solutions-google-compute-engine-orchestrator,代码行数:26,代码来源:GceApiUtils.java

示例7: doGet

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  String trackingId = System.getenv("GA_TRACKING_ID");
  URIBuilder builder = new URIBuilder();
  builder
      .setScheme("http")
      .setHost("www.google-analytics.com")
      .setPath("/collect")
      .addParameter("v", "1") // API Version.
      .addParameter("tid", trackingId) // Tracking ID / Property ID.
      // Anonymous Client Identifier. Ideally, this should be a UUID that
      // is associated with particular user, device, or browser instance.
      .addParameter("cid", "555")
      .addParameter("t", "event") // Event hit type.
      .addParameter("ec", "example") // Event category.
      .addParameter("ea", "test action"); // Event action.
  URI uri = null;
  try {
    uri = builder.build();
  } catch (URISyntaxException e) {
    throw new ServletException("Problem building URI", e);
  }
  URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
  URL url = uri.toURL();
  fetcher.fetch(url);
  resp.getWriter().println("Event tracked.");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:29,代码来源:AnalyticsServlet.java

示例8: perform

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Override
public Page perform(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    int id = Integer.parseInt(req.getParameter("id"));

    String remoteAddr = req.getRemoteAddr();
    Objectify ob = DefaultServlet.getObjectify();

    String secretParameter = getSetting(ob, "ReCaptchaPrivateKey", "");
    String recap = req.getParameter("g-recaptcha-response");
    URLFetchService us = URLFetchServiceFactory.getURLFetchService();
    URL url = new URL(
            "https://www.google.com/recaptcha/api/siteverify?secret=" +
            secretParameter + "&response=" + recap + "&remoteip=" + req.
            getRemoteAddr());
    HTTPResponse r = us.fetch(url);
    JSONObject json = null;
    try {
        json = new JSONObject(new String(r.getContent(), Charset.
                forName("UTF-8")));

        String s;
        if (json.getBoolean("success")) {
            Editor e = ob.query(Editor.class).filter("id =", id).get();
            s = "Email address: " + e.name;
        } else {
            s = "Answer is wrong";
        }

        return new MessagePage(s);
    } catch (JSONException ex) {
        throw new IOException(ex);
    }
}
 
开发者ID:tim-lebedkov,项目名称:npackd-gae-web,代码行数:35,代码来源:ReCaptchaAnswerAction.java

示例9: checkURL

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
/**
 * Checks an URL using the Google Safe Browsing Lookup API
 *
 * @param ofy Objectify
 * @param url this URL will be checked
 * @return GET_RESP_BODY = “phishing” | “malware” | "unwanted" |
 * “phishing,malware” | "phishing,unwanted" | "malware,unwanted" |
 * "phishing,malware,unwanted" | ""
 * @throws java.io.IOException there was a communication problem, the server
 * is unavailable, over quota or something different.
 */
public static String checkURL(Objectify ofy, String url) throws IOException {
    try {
        URL u = new URL(
                "https://sb-ssl.google.com/safebrowsing/api/lookup?client=npackdweb&key=" +
                getSetting(ofy, "PublicAPIKey", "") +
                "&appver=1&pver=3.1&url=" +
                NWUtils.encode(url));
        URLFetchService s = URLFetchServiceFactory.getURLFetchService();

        HTTPRequest ht = new HTTPRequest(u);
        HTTPResponse r = s.fetch(ht);
        int rc = r.getResponseCode();
        if (rc == 200) {
            return new String(r.getContent());
        } else if (rc == 204) {
            return "";
        } else if (rc == 400) {
            throw new IOException(new String(r.getContent()));
        } else {
            throw new IOException(
                    "Unknown exception from the Google Safe Browsing API");
        }
    } catch (MalformedURLException ex) {
        throw new IOException(ex);
    }
}
 
开发者ID:tim-lebedkov,项目名称:npackd-gae-web,代码行数:38,代码来源:NWUtils.java

示例10: fetchNonExistentLocalAppThrowsException

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Test(expected = IOException.class)
public void fetchNonExistentLocalAppThrowsException() throws Exception {
    URL selfURL = new URL("BOGUS-" + appUrlBase + "/");
    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults()
        .doNotFollowRedirects()
        .setDeadline(10.0);
    HTTPRequest httpRequest = new HTTPRequest(selfURL, HTTPMethod.GET, fetchOptions);
    URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse httpResponse = urlFetchService.fetch(httpRequest);
    fail("expected exception, got " + httpResponse.getResponseCode());
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:12,代码来源:URLFetchServiceTest.java

示例11: testBasicOps

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Test
public void testBasicOps() throws Exception {
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();

    URL adminConsole = findAvailableUrl(URLS);
    HTTPResponse response = service.fetch(adminConsole);
    printResponse(response);

    URL jbossOrg = new URL("http://www.jboss.org");
    if (available(jbossOrg)) {
        response = service.fetch(jbossOrg);
        printResponse(response);
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:15,代码来源:URLFetchTest.java

示例12: testPayload

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Test
public void testPayload() 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("Tralala".getBytes(UTF_8));

    HTTPResponse response = service.fetch(req);
    String content = new String(response.getContent());
    Assert.assertEquals("Hopsasa", content);
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:15,代码来源:URLFetchTest.java

示例13: testFollowRedirectsExternal

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
@Test
public void testFollowRedirectsExternal() throws Exception {
    final URL redirectUrl = new URL("http://google.com/");
    final String expectedDestinationURLPrefix = "http://www.google.";

    FetchOptions options = FetchOptions.Builder.followRedirects();

    HTTPRequest request = new HTTPRequest(redirectUrl, HTTPMethod.GET, options);
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse response = service.fetch(request);
    String destinationUrl = response.getFinalUrl().toString();
    assertTrue("Did not get redirected.", destinationUrl.startsWith(expectedDestinationURLPrefix));
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:14,代码来源:FetchOptionsBuilderTest.java

示例14: getResponsePUT

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
public static String getResponsePUT(String url, Map<String, String> params, String json, Map<String, String> headers) {
	int retry = 0;
	while (retry < 3) {
		long start = System.currentTimeMillis();
		try {
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			String urlStr = ToolString.toUrl(url.trim(), params);
			logger.debug("PUT : " + urlStr);
			HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.PUT, FetchOptions.Builder.withDeadline(deadline));
			if (headers != null) {
				for (String header : headers.keySet()) {
					httpRequest.addHeader(new HTTPHeader(header, headers.get(header)));
				}
			}
			httpRequest.setPayload(json.getBytes());
			HTTPResponse response = fetcher.fetch(httpRequest);
			return processResponse(response);
		} catch (Throwable e) {
			retry++;
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			} else if (retry < 3) {
				logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage());
			} else {
				logger.error(e.getClass() + "\n" + ToolString.stack2string(e));
			}
		}
	}
	return null;
}
 
开发者ID:rafali,项目名称:flickr-uploader,代码行数:31,代码来源:HttpClientGAE.java

示例15: export

import com.google.appengine.api.urlfetch.URLFetchService; //导入方法依赖的package包/类
/**
 * Exports a repository.
 *
 * @param ob Objectify instance
 * @param recreate true = recreate the repository if it already exists
 * @return the repository with blobFile != null
 * @throws java.io.IOException any error
 */
public static Repository export(Objectify ob, boolean recreate)
        throws IOException {
    final String tag = "vim-org";

    URLFetchService s = URLFetchServiceFactory.getURLFetchService();
    HTTPRequest request =
            new HTTPRequest(
                    new URL(
                            "https://bitbucket.org/vimcommunity/vim-pi/raw/51a23481851685dda95cae07e97185870cf67723/db/vimorgsources.json"));
    request.getFetchOptions().setDeadline(60.0);
    HTTPResponse response = s.fetch(request);
    byte[] content = response.getContent();
    byte[] content2 = Arrays.copyOf(content, content.length - 1);
    content2[content2.length - 2] = '}';

    ByteArrayInputStream bais = new ByteArrayInputStream(content2);
    InputStreamReader isr = new InputStreamReader(bais, "UTF-8");
    JsonReader jr = new JsonReader(isr);
    jr.setLenient(true);
    JsonElement e = new JsonParser().parse(jr);

    Repository r = ob.find(new Key<Repository>(Repository.class, tag));
    if (r == null) {
        r = new Repository();
        r.name = tag;
        NWUtils.saveRepository(ob, r);
    }

    final GcsService gcsService =
            GcsServiceFactory.createGcsService(RetryParams
                    .getDefaultInstance());

    GcsFilename fileName = new GcsFilename("npackd", tag + ".xml");

    boolean exists = gcsService.getMetadata(fileName) != null;

    if (r.blobFile == null || recreate || !exists) {
        GcsOutputChannel outputChannel =
                gcsService.createOrReplace(fileName,
                        GcsFileOptions.getDefaultInstance());

        Document d = toXML(e);

        OutputStream oout = Channels.newOutputStream(outputChannel);
        NWUtils.serializeXML(d, oout);
        NWUtils.serializeXML(d, System.out);
        oout.close();

        r.blobFile =
                "/gs/" + fileName.getBucketName() + "/" +
                fileName.getObjectName();

        NWUtils.saveRepository(ob, r);
    }

    return r;
}
 
开发者ID:tim-lebedkov,项目名称:npackd-gae-web,代码行数:66,代码来源:ExportDefaultVimRepAction.java


注:本文中的com.google.appengine.api.urlfetch.URLFetchService.fetch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。