Forum Discussion
Ayyappa_P
Jun 23, 2020Copper Contributor
One Drive Access Error: No Such Host is Known: SocketException (11001)
Hi,
Some times, while generating access token, I'm getting the following error.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.Net.Http.HttpRequestException: No such host is known.
---> System.Net.Sockets.SocketException (11001): No such host is known.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
This is the code, I'm using.
string strURLForAccessToken = "https://login.microsoftonline.com/" + strOneDriveTenantId + "/oauth2/token";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, strURLForAccessToken)
{
Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("client_id", strClientId),
new KeyValuePair<string, string>("client_secret", strClientSecret),
new KeyValuePair<string, string>("resource", "https://graph.microsoft.com"),
new KeyValuePair<string, string>("username", strUserName),
new KeyValuePair<string, string>("password", strPassword)
})
};
try
{
HttpResponseMessage strResponse = objHttpClient.SendAsync(request).GetAwaiter().GetResult();
string strResult = strResponse.Content.ReadAsStringAsync().Result;
strNewAccessToken = JObject.Parse(strResult)["access_token"].ToString();
If I try after some time, with the same values, then this is fine. Why it is throwing error for first time?
Please Help.
Feel free to suggest any area of improvement in the above code.
No RepliesBe the first to reply