简体   繁体   中英

C# - Azure Function issue

I am new to coding and not familar with C#. However, I have a PHP script that I am trying to convert to C# to use in Azure Function that will trigger every 15 mins. I have the first section and function of the code and it compiles and produces a success in the Azure Function console but does not provide the result in an output. below is the code:

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));
}
}
}
    }

If I remove the var authorizationCode I get error that it does not have a namespace and if I return it or response I do not get any output.

Help would be appreciated.

httpClient.GetAsync is an asynchronous statement, which means that the code is continuing without waiting for the webrequest to finish. Wait for GetAsync and then you can continue:

var task = httpClient.GetAsync("https://auth.bullhornstaffing.com/oauth/authorize?client_id=****type=code&username=*****&password=*****&action=Login");
task.Wait();
var response = task.Result;

There are also two brackets too much (lines 12&13) and I guess you want to access the ContentLocation of the httpContent?

Probably that's the right one:

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));
            }
        }
    }
}

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