whisper
whisper

Reputation: 143

How to use the com.google.api.client.extensions.appengine.http.UrlFetchTransport to get an index.html

I don't know how to use this API to get an index.html

Please show me a sample.

This is my complete code, HTTP error 500

package com.webrt;

import java.io.IOException;
import javax.servlet.http.*;

import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;

@SuppressWarnings("serial")
public class WebRTServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/plain");

        UrlFetchTransport HTTP_TRANSPORT = new UrlFetchTransport();
        HttpRequestFactory httprequestFactory = HTTP_TRANSPORT
                .createRequestFactory();
        GenericUrl url = new GenericUrl("http://www.google.com");
        HttpRequest request = httprequestFactory.buildGetRequest(url);
        String index = request.execute().parseAsString();
        System.out.println(index);
    }
}

Upvotes: 1

Views: 4162

Answers (1)

Nick
Nick

Reputation: 9154

You can find samples here and information about the library here. To grab a file using the library, just make a get request for that file. An index.html page is no exception.

Here's a basic sample:

The HTTP Transport you use shouldn't matter in this context. UrlFetchTransport is useful for AppEngine apps, but it's just the means of making the HTTP call. Regardless of which transport you set the constant HTTP_TRANSPORT to be (UrlFetch, NetHttp, etc.), the following code should work.

HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl("http://example.iana.org/index.html");
HttpRequest request = requestFactory.buildGetRequest(url);
String index = request.execute().parseAsString();

From here you could save index to a file, print it, or whatever you wanted. It will be the full file as the browser would see it.

Upvotes: 2

Related Questions