简体   繁体   中英

Post request timing out online, but not locally [Google App Engine]

I have a problem with Google App Engine timing out when I preform a post request to a specified URL. When I do a test request over at hurl.it , it works perfectly fine. When I run the code in a local server, runs perfectly fine. When I deploy it to Google App Engine and test it online, it times out no matter how much timeout limit I give it (tested up to 1 minute). I have no clue what's causing it to bug out online and not locally. Here's the code and the stack trace I get when it errors out:

package com.servlet.thing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ServletThingServlet extends HttpServlet {
private static final String PASS = "testpassword123";

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    URL url = new URL("https://api.roblox.com/login/v1");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    //URLConnection con = url.openConnection();
    con.setConnectTimeout(60000);
    // Doesn't get past here online
    resp.getWriter().println("connected");

    con.setDoOutput(true);
    con.setRequestMethod("POST");
    resp.getWriter().println("checkpoint 1");

    OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
    writer.write("username=TestPromotionAcc&password=" + PASS);
    writer.flush();
    writer.close();
    resp.getWriter().println("checkpoint 2");

    HashMap<String, List<String>> map = (HashMap<String, List<String>>) con.getHeaderFields();

    if (con.getResponseCode() == HttpURLConnection.HTTP_OK || con.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        StringBuffer response = new StringBuffer();
        String line;

        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

        System.out.println("--- HEADER FIELDS ---\n");

        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("set-cookie")) {
                System.out.println("Value : " + entry.getValue());
                resp.getWriter().println("Set cookies: " + entry.getValue());
            }
        }

        reader.close();
        System.out.println(response.toString());
    } else {
        resp.getWriter().println(con.getResponseCode() + ": " + con.getResponseMessage());
    }
}
}

And here's the stack trace:

Uncaught exception from servlet java.net.SocketTimeoutException: Timeout while fetching URL: http://api.roblox.com/login/v1 at com.google.appengine.api.urlfetch.URLFetchServiceImpl.convertApplicationException(URLFetchServiceImpl.java:179) at com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:45) at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.fetchResponse(URLFetchServiceStreamHandler.java:543) at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getInputStream(URLFetchServiceStreamHandler.java:422) at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getResponseCode(URLFetchServiceStreamHandler.java:275) at com.servlet.thing.ServletThingServlet.doGet(ServletThingServlet.java:30) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.Servlet Holder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org .mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:257) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923) at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:145) at com.google.apphosting.runt ime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:511) at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:446) at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:453) at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:274) at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:312) at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:304) at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:450) at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:235) at java.lang.Thread.run(Thread.java:745)

In my experience the deadlines aren't enforced locally. Timeouts occur if your request exceeds 60 seconds . To overcome this I'd wrap the request in a task which have a more liberal 10 minutes to get things done. Other things to check - do you have an app instance running, or is app engine having to spin up a new instance to handle the openConnection ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM