1

I have a web service where I can GET, POST, PUT and DELETE data from the Postman tool or directly from the google chrome browser. i.e. if I do GET in postman, I am going to get the information of the map as you can see in the following image GET map.

Now, I want to the same GET, but from my own program in visual studio. I have created a visual studio console program with the following code:

static void Main(string[] args)
{
    Console.WriteLine("Welcome to the API program!");
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://161.30.96.250:51615/");

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    Console.WriteLine("Before the query!");
    HttpResponseMessage response =  client.GetAsync("api/1.0/config/element/satellite").Result;

    Console.WriteLine("After reponse!");
    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine(response.ToString());
    }
    else
    {
        //Something has gone wrong, handle it here
    }
}

When I Start the program, is complaining about this line: "HttpResponseMessage response = client.GetAsync("api/1.0/config/element/satellite").Result;". Complaining about the following ERROR.

Does anyone know how to solve this problem?

I think that one problem could be that I need to put the user and the password to access to this web service. I tried to solve that as it is explained in the solution of this question(How to use credentials in HttpClient in c#?), but it didn't work neither.

Added: Some of you asked to add the inner exception. Here you have the inner exception.

System.AggregateException was unhandled
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at ConsoleApplication1.Program.Main(String[] args) in H:\visualstudio\webAPIquery\ConsoleApplication1\ConsoleApplication1\Program.cs:line 99
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       BareMessage=Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section.
       HResult=-2146232062
       Line=0
       Message=Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section.
       Source=System
       StackTrace:
            at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
            at System.Net.WebRequest.get_InternalDefaultWebProxy()
            at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
            at System.Net.HttpWebRequest..ctor(Uri uri, Boolean returnResponseOnFailureStatusCode, String connectionGroupName, Action`1 resendRequestContent)
            at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
            at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       InnerException: 
            ErrorCode=10022
            HResult=-2147467259
            Message=An invalid argument was supplied
            NativeErrorCode=10022
            Source=System
            StackTrace:
                 at System.Net.SafeCloseSocketAndEvent.CreateWSASocketWithEvent(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, Boolean autoReset, Boolean signaled)
                 at System.Net.NetworkAddressChangePolled..ctor()
                 at System.Net.AutoWebProxyScriptEngine.AutoDetector.Initialize()
                 at System.Net.AutoWebProxyScriptEngine.AutoDetector.get_CurrentAutoDetector()
                 at System.Net.AutoWebProxyScriptEngine..ctor(WebProxy proxy, Boolean useRegistry)
                 at System.Net.WebProxy.UnsafeUpdateFromRegistry()
                 at System.Net.WebProxy..ctor(Boolean enableAutoproxy)
                 at System.Net.Configuration.DefaultProxySectionInternal..ctor(DefaultProxySection section)
                 at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
            InnerException: 

This post is already solved. I was running the program in the network of our company and because of the security that we have, it was not working. I don't know how mark it as solved, let me know how to do it please.

Community
  • 1
  • 1
Gaizka
  • 11
  • 3
  • 1
    Click on "View Detail..." on that expection dialog to see the inner exception. AggregateException is a very high-level error. – Arian Motamedi Dec 01 '15 at 17:55
  • An `AggregateException` is just a collection of multiple exceptions; you need to inspect the inner exceptions to know what is really going on (and post the text of the exception, not a screenshot). It probably has a lot to do with you not `await`ing `GetAsync`. – Paul Abbott Dec 01 '15 at 17:58
  • @PaulAbbott Essentially he is `await`ing it via the `.Result` call at the end, just calling it synchronously instead of asynchronously. – Ron Beyer Dec 01 '15 at 18:14
  • The screenshot shows an alternative url, is that intentional? – Caramiriel Dec 01 '15 at 19:44
  • Simply dereferencing `.Result` doesn't make it synchronous. He needs to call `.GetAwaiter().GetResult()` instead, or use the `await` keyword. –  Dec 01 '15 at 19:47
  • 2
    @Gaizka Add answer to your question and write your answer and mark it as acepted – Ali Foroughi Dec 02 '15 at 10:10
  • @gaizka to mark a question as solved write an answer and mark it as accepted – Thomas Dec 02 '15 at 10:27
  • This post is already solved. I was running the program in the network of our company and because of the security that we have, it was not working. But the code is ok. Thanks @AliForoughi and Thomas – Gaizka Dec 02 '15 at 11:14

0 Answers0