[英]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.