[英]C# - Azure Function issue
我是编码新手,并不熟悉C#。 但是,我有一个PHP脚本,试图将其转换为C#以在Azure Function中使用,它将每15分钟触发一次。 我具有代码的第一部分和功能,它可以在Azure Function控制台中编译并产生成功,但不会在输出中提供结果。 下面是代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
{
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.AllowAutoRedirect = false;
using (var httpClient = new HttpClient(httpClientHandler))
{
var response =
httpClient.GetAsync("https://auth.bullhornstaffing.com/oauth/authorize?
client_id=****type=code&username=*****&password=*****&action=Login").Result;
if (response.StatusCode == HttpStatusCode.Found)
{
var redirectUrl = response.Headers.Location;
var startIndex = redirectUrl.Query.IndexOf("code=") + 5;
var endIndex = redirectUrl.Query.IndexOf("&", startIndex);
var authorizationCode = (redirectUrl.Query.Substring(startIndex, endIndex -
startIndex));
}
}
}
}
如果删除varauthorizationCode,则会收到错误消息,提示它没有名称空间,如果返回它或响应,则不会得到任何输出。
帮助将不胜感激。
httpClient.GetAsync是一个异步语句,这意味着代码将继续运行而无需等待webrequest完成。 等待GetAsync,然后您可以继续:
var task = httpClient.GetAsync("https://auth.bullhornstaffing.com/oauth/authorize?client_id=****type=code&username=*****&password=*****&action=Login");
task.Wait();
var response = task.Result;
还有两个括号太多(第12和13行),我想您想访问httpContent的ContentLocation吗?
也许那是正确的:
using System;
using System.Net;
using System.Net.Http;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.AllowAutoRedirect = false;
using (var httpClient = new HttpClient(httpClientHandler))
{
log.Info("get async...");
var task = httpClient.GetAsync("https://auth.bullhornstaffing.com/oauth/authorize?client_id=****type=code&username=*****&password=*****&action=Login");
task.Wait();
var response = task.Result;
var httpContent = response.Content;
log.Info("Result: " + httpContent.Headers.ContentLocation);
if (response.StatusCode == HttpStatusCode.Found)
{
var redirectUrl = httpContent.Headers.ContentLocation;
var startIndex = redirectUrl.Query.IndexOf("code=") + 5;
var endIndex = redirectUrl.Query.IndexOf("&", startIndex);
var authorizationCode = (redirectUrl.Query.Substring(startIndex, endIndex - startIndex));
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.