从Java ManagedVM url获取请求

时间:2015-12-08 20:51:22

标签: java google-app-engine urlfetch

我有一个在App Engine的ManagedVM上运行的Java应用程序。我目前正试图让它在我的应用程序中调用另一个模块。此模块使用X-Appengine-Inbound-Appid标头的值来确保安全性,以确保只有其他模块可以调用该端点。但是,我的请求中未正确设置此标头。我已按照urlfetch说明操作,包括按照说明设置connection.setInstanceFollowRedirects(false);,但标题仍未设置。

关于可能出现什么问题的任何想法?

    URL url = new URL("https", hostname, "/path");

    HttpURLConnection connection = (HttpURLConnection)url.openConnection();

    connection.setInstanceFollowRedirects(false);

    String value = req.getHeader("My-Header");
    if (value != null) {
        connection.setRequestProperty("My-Header", value);
    }

    int respCode = connection.getResponseCode();
    if (respCode != 200) {
        resp.sendError(respCode, "invalid response code from upstream");
        return;
    }

    // use the successful response...

1 个答案:

答案 0 :(得分:1)

我能够直接使用URLFetchService而不是#!/usr/bin/env python from datetime import datetime import pytz # $ pip install pytz utc_offset = datetime.now(pytz.timezone('Asia/Calcutta')).utcoffset() print(utc_offset) # -> 5:30:00 来解决此问题。我的猜测是,在托管虚拟机应用程序中,您可以使用url软件包进行常规网络请求(没有设置String regex = "(?:\\brules\\s+\\{|(?!^)\\G)\\s+([\\w-]+)"; final Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(output); while (matcher.find()) { System.out.println(matcher.group(1)); } 标头),并且必须直接使用URLFetchService才能通过网址获取基础设施。