使用c#的Lambda函数无法调用外部HTTPS API

时间:2018-11-04 19:25:14

标签: aws-lambda alexa-skills-kit

我试图从用c#编写的AWS lambda函数调用外部API。Lamda函数以“无VPC”模式部署。我是从Alexa技能调用此功能的。该代码适用于http请求,但不适用于https。

以下代码在我使用http://www.google.com时效果很好。但是,如果我将HTTP替换为https,那么我会在云监视中看到错误消息

  

“在完成请求之前,进程已退出。”

即使在catch中写入的日志也未在Cloud Watch中记录。 请指教。

    public class Function
{
    public const string INVOCATION_NAME = "bingo";

    public async Task<SkillResponse> FunctionHandler(SkillRequest input, ILambdaContext context)
    {

        var requestType = input.GetRequestType();
        if (requestType == typeof(IntentRequest))
        {
            string response = "";
            IntentRequest request = input.Request as IntentRequest;
            response += $"About {request.Intent.Slots["carmodel"].Value}";

            try
            {
                using (var httpClient = new HttpClient())
                {
                    Console.WriteLine("Trying to access internet"); 
                    //var resp=httpClient.GetAsync("http://www.google.com").Result // this works perfect!                   
                    var resp = httpClient.GetAsync("https://www.google.com").Result; // this throws error
                    Console.WriteLine("Call was successful");
                }                
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception from main function " + ex.Message);
                Console.WriteLine(ex.InnerException.Message);
                Console.WriteLine(ex.StackTrace);

            }            
            return MakeSkillResponse(response, true);
        }
        else
        {
            return MakeSkillResponse(
                    $"I don't know how to handle this intent. Please say something like Alexa, ask {INVOCATION_NAME} about Tesla.",
                    true);
        }
    }


    private SkillResponse MakeSkillResponse(string outputSpeech, bool shouldEndSession,
        string repromptText = "Just say, tell me about car models to learn more. To exit, say, exit.")
    {
        var response = new ResponseBody
        {
            ShouldEndSession = shouldEndSession,
            OutputSpeech = new PlainTextOutputSpeech { Text = outputSpeech }
        };

        if (repromptText != null)
        {
            response.Reprompt = new Reprompt() { OutputSpeech = new PlainTextOutputSpeech() { Text = repromptText } };
        }

        var skillResponse = new SkillResponse
        {
            Response = response,
            Version = "1.0"
        };
        return skillResponse;
    }
}

1 个答案:

答案 0 :(得分:0)

问题已解决。 System.net.http v4.3.4与Core dot net v1不完全兼容。因此,出站http呼叫有效,但https呼叫无效。更改System.net.http的版本可以解决此问题。