Reputation: 4848
I have a very simple test app that creates a WebClient and gets a response from a third-party API (sensitive info removed):
using (WebClient client = new WebClient())
{
var baseAddress = new Uri("https://test-url");
client.Headers["Content-Type"] = "application/json";
var parms = Encoding.Default.GetBytes(
"{ \"username\":\"<USERNAME>\"," +
"\"password\":\<PASSWORD>\"," +
"\"client_id\":\"<CLIENT_ID>\"," +
"\"client_secret\":\"<CLIENT_SECRET>\"" +
"}"
);
// Get access token
var responseObject = new ResponseObject();
var responseBytes = new byte[] { };
string responseBody;
try
{
responseBytes = client.UploadData(baseAddress, "POST", parms);
responseBody = Encoding.UTF8.GetString(responseBytes);
responseObject = JsonConvert.DeserializeObject<ResponseObject>(responseBody);
}
catch (WebException ex)
{
var resp = (HttpWebResponse)ex.Response;
Console.WriteLine(resp.StatusDescription);
}
}
This code works perfectly when I run it locally, multiple times. I get a response back and I'm able to extract the information I need from it.
However, once I deploy this code to Azure, I try to call the API with POSTMAN and I get an error telling me that there's an "Object reference not set to an instance of an object." error on this line in the try/catch:
var resp = (HttpWebResponse)ex.Response;
So, I attached a remote debugger to the API and stepped through this small bit of code. What's weird to me is that I can see right when the program jumps into the catch
portion of the code, but there is no ex
Exception object. What I mean by that is that I can hover over the ex
variable in debug and nothing appears. Even hovering over ex.Response
shows nothing. So, since there is nothing there (i.e. ex
doesn't appear to exist, not even a null
value) the application bombs with the null reference error.
My thought is that maybe something in Azure is blocking the URL, since I can run this locally without issue. I asked our network guys here about it and they said there shouldn't be any kind of constraints set up that they are aware of to prevent the call from going out. The weird thing is that I turned on diagnostic logging in Azure for this app and nothing is showing up. Even though I can hit the app with a browser or PostMan, nothing is appearing in the log. So, that's another weird thing. I'd expect to be able to see something.
Anyway, I'm hoping that perhaps someone has ran into something like this before and can point me to an article/solution/blog that may shed light on this and get me to a working solution.
Thanks!
EDIT: Added a screenshot of the error message as it appears in the browser:
EDIT: Added the line on which the app is throwing the exception below:
responseBytes = client.UploadData(baseAddress, "POST", parms);
Immediately after trying to execute this line, it jumps to the catch
, but there is no WebException, it appears.
EDIT: I've found the solution and added it as an answer below.
Upvotes: 2
Views: 1139
Reputation: 4848
I found a solution that worked and it took one line of code:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
So, basically, I just had to set the SecurityProtocol. I don't know how this was working locally without it, but running it on Azure requires it to be set, apparently.
Upvotes: 2