Using GET request causes bot dialog to fail - c#

I've connected my bot application to the direct line API which is published with Azure. I am currently testing the application with a command line client application, the bot framework emulator, and the dev.botframework.com homepage for my bot.
Everything works correctly until I attempt to submit a GET request to a REST API. I've tested the GET API request in a separate project and it works correctly and the GET request worked prior to implementing the direct line channel. Is there anything I need to be aware of when making http requests with the direct line on the bot side?
Code in question
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", headerParam);
var response = client.GetAsync(new Uri("someUrl.com/api/v1/auth")).Result;
string content = response.Content.ReadAsStringAsync().Result;
var jo = JObject.Parse(content);
this.token = jo["Result"]["Token"].ToString();
}
await context.PostAsync(this.token);
the line that actually causes the failure is
var response = client.GetAsync(new Uri("someUrl.com/api/v1/auth")).Result;
Also is there an easier way to debug a project when it's published to azure and running direct line API?

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions ipaddress
I have tried to invoke my custom REST API within my bot application back-end, then I could leverage Remote debugging web apps to retrieve the result from my bot application hosted on Azure web app as follows:
After searching for the related issue, I found that there be limitation for the number of sockets of your current app service plan when creating the new outgoing connections. You could try to scale up your App Service plan or create a new web app to isolate this issue. For more details, you could refer to this similar issue and this blog.

Related

HttpClient runs in local but stuck in AWS Windows Server

I'm makin an API with net core 2.1 and run into a issue where I CAN gen a correct response in my local machine (even running the published files) BUT gen stuck when i upload and run the app in a aws ec2 windows server 2012.
I've tried using the IHttpClientFactory, adding the httpclient at services collection, making a service whith the AddHttpClient and running the release profile in my pc and with every method it runs fine in my pc but hangs in aws ec2 windows server 2012.
I'm running the app in aws ec2 with $netcore ./application.dll for now.
my pc has netcore 2.2.300 and aws 2.2.400
Here is the part of the code I use in my controller (also try with making a service):
WebCuitRequest req = new WebCuitRequest {cuit = "cuit", token = "Yt25zBH" };
string json = JsonConvert.SerializeObject(req);
string baseUrl = "url.com/getthis";
StringContent queryString = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage res = await _client.PostAsync(baseUrl, queryString);
//------i dont get here (no error, no response)
HttpContent content = res.Content;
string data = await content.ReadAsStringAsync();
return data;
I expect an error or response but none is returned (i have a try/catch and some Console.write that never get reached).
Update 1 (solution):
Moving the API I was trying to reach to a hosting without cloudfare solved the problem. It seems that cloudfare was blocking the aws instance (but no my pc) and for some reason I didn't get a response or error. I must clarify that I have access to the target API and the posibility to move it, I don't know what can be done if you can't do changes in that env.
Tanks to Robert Perry who get me to evaluate another part on the situation.
This is most likely caused by Azures' default security settings. I believe it locks out calls to external IP addresses by default - its worth checking this article and matching it to how your Azure is configured: https://learn.microsoft.com/en-us/azure/storage/common/storage-network-security
Moving the API I was trying to reach to a hosting without cloudfare solved the problem.
It seems that cloudfare was blocking the aws instance (but no my pc) and for some reason I didn't get a response or error.
I must clarify that I have access to the target API and the posibility to move it, I don't know what can be done if you can't do changes in that env.

Google Cloud API not returning any response

Background information:
I'm trying to create a PoC for Google Cloud Vision API using their .NET library.
What I have done:
Create a simple console apps with the following code for Vision API.
GoogleCredential credential = GoogleCredential.FromFile(ConfigurationManager.AppSettings["GoogleCredentialFile"]);
Grpc.Core.Channel channel = new Grpc.Core.Channel(Google.Cloud.Vision.V1.ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = Google.Cloud.Vision.V1.ImageAnnotatorClient.Create(channel);
var image = Google.Cloud.Vision.V1.Image.FromFile(#"C:\Users\u065340\Documents\sample.jpg");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
result = annotation.Description;
}
Problem:
The line client.DetectLabels(image) gets stuck for a long time before ultimately throwing the error Deadline Exceeded.
My code sits behind a corporate proxy, but I have validated that it is not blocking internet access because I can call https://vision.googleapis.com/$discovery/rest?version=v1 from the same apps and get its JSON response just fine.
Any suggestions?
After digging around through github issues related to proxies as suggested by Jon Skeet, I found that Google Cloud Client APIs can be generally divided into 2 categories (Ref: here): REST-based HTTP 1.1 with JSON and gRPC.
For APIs associated as REST-based, there should be no issue with proxies. The problem starts to appear when we are using gRPC-based APIs such as Google Cloud Vision and Google Speech. In gRPC, we need to explicitly provide our proxy server information.
For those using Java Client, it seems we still can't set proxy properly because it will eventually be ignored, and causing the Deadline Exceeded error. This issue is already well known and can be found at here and further traced into here.
The Google team has determined that it is indeed a bug, and the status remains Open.
As for C# Client, we can set proxy information using gRPC Environment Variables which is documented in here. The code is Environment.SetEnvironmentVariable("http_proxy", <your_proxy_server>);
After I set the http_proxy environment variable pointing to my proxy server, all is well again. I get the expected output "This API needs Billing Account".
Many thanks to Jon Skeet for pointing me in the right direction :D

Got error when access https URL using HttpClient

i'm using Visual Studio to build Android application. I'm using REST API to communicate the app with the server, and later i got this error
System.Net.Http.HttpRequestException: An error occurred while sending the request
this is my codes
var client = new HttpClient();
client.DefaultRequestHeaders.Add(defaultData.headerName, defaultData.headerData);
var json = await client.GetStringAsync("https://example.com/api");
var items = JsonConvert.DeserializeObject<ArdilesmetroClass>(json);
This problem occurs when i try to accessing https url and it works fine when accessing http. What i need to add in my codes?
Done, i have the answer.Go to your Project Properties->Android Options-> Click Advanced. You need to change SSL/TLS implementation to
Native TLS 1.2+

How can I get more information about a fault in system.net.http?

I'm using HttpClient from System.Net.Http on nuget to make a request from a module (assembly) written in c#. This needs to work with a legacy application in Windows XP so we're using .Net Framework 4.0.
I can successfully send a https request to our server from my Windows 10 machine; however, when I test it from Windows XP in a VM, I get a fault. I can request websites like https://google.com but our https site causes a fault. On top of all this, the res.StackTrace is null! The Message I get from obj.Exception.InnerException is An error ocurred while sending the request. I can however get to our site in a browser in the VM without trouble. So why can't I get a StackTrace? Could this be some kind of ssl trust issue (our site has an ssl certificate from godaddy)?
var client = new HttpClient();
client.BaseAddress = new Uri(SERVER_BASE_URL); // https://...
// Works when BaseAddress="https://google.com" but not our https site
return client.GetAsync("/").ContinueWith(res =>
{
if (res.IsFaulted)
// res.StackTrace is empty >:(
MessageBox.Show("Faulted");
else
MessageBox.Show("Success");
});

Windows Phone app can't access a web service

I'm writing an application for Windows Phone 8.1. I have hosted a WCF service on IIS Express on my local machine and configured it to be accessible from the WP8 device that I'm using for testing (done all the endpoint config, added firewall rules etc). I have no problem accessing my service from the smartphone's web browser, so I guess it is not a server config issue. But when I do the same request from within my app, I get a 404 response.
Here's the code that sends the request.
client = new HttpClient();
client.BaseAddress = new Uri("http://192.144.1.104:50102/EntityService.svc/");
var builder = new StringBuilder();
builder.Append("player/register?id=");
builder.Append(deviceId.ToString());
builder.Append("&nickname=");
builder.Append(nickname);
var request = new HttpRequestMessage(HttpMethod.Get, builder.ToString());
var response = await client.SendAsync(request);
var dataString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<ResultDTO>(dataString);
I want to point this out once again, when I copy the formed request link in the phone's web browser, it works fine. I have no idea why it gives me 404 in the application. Does anyone know what's the reason for this?
Thanks in advance.
P.S. I have checked, and IIS Express did not log any requests that are sent from the app.
P.P.S. In the appmanifest, Internet (Client/Server) capability is checked, so it shouldn't be an application permission issue.

Categories