locked
what are the steps to Query WebAPI using HTTP Get method on C# RRS feed

  • Question

  • User-170056757 posted

    Tool: VS 2013 IDE
    Language: C# 
    Question:  What are the steps to query an API using HTTP Get method on C#?

    I would appreciate if you list objects/classes and the logical steps to get data by parsing xml document 
     

    Friday, November 4, 2016 8:06 PM

Answers

All replies

  • User-654786183 posted

    The following link has a very good tutorial on how to call Web API using HTTP GET with C#

    https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Friday, November 4, 2016 8:49 PM
  • User-170056757 posted

    Thanks; the link doesn't address my question.  it's irrelevant.  I find the following link fulfill my goals for this assignment because the author list a number of class object to use and how to call those classes and parse an xml using get method
    https://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee   

    Friday, November 4, 2016 9:56 PM
  • User753101303 posted

    Hi,

    HttpClient can handle authentication, allows to get and post data models and can use XML for serialization (or could it be a SOAP based service ?) so this is likely as well where I would start.

    To start with, you'll use that on your web site to call a 3rd part API? Have you tried something? If the service exists,which kind of authentication is used? It looks rather that it expects these tokens to be provided as arguments to the service call. You can call it using a browser?

    Edit: or maybe create a web API sample service and call it from some other test page to first familiarize yourself with that? Could you first clarify if this is an existing 3rd party service?

    Saturday, November 5, 2016 6:51 PM
  • User-170056757 posted

    Yes, I made positive progress; I built aspx and cs page and I am facing technical issue

    Error: System.NullReferenceException: Object reference not set to an instance of an object. at WebReg._Default.getattributevalue(String id, XmlDocument doc) in Default.aspx.cs: line 55 -->   XmlNodeList nodelist = root.SelectNodes("//sign", nsmgr);

    Saturday, November 5, 2016 7:27 PM
  • User753101303 posted

    NullReferenceException means you are trying to call an object member on a null object. Are you sure root is not null ? Use the VS debugger to check that when the exception happens.

    Not sure which kind of XML document you are using. If this XML document is just a serialized data model, it could be handled for you as .NET includes this capability out of the box (search for serialization/deserialization). And so Web API as well as HttpClient are building upon that so that the code you have to write to expose or consume a service, will just use directly your data model, the serialization/deserialization code having been written already for you.

    Saturday, November 5, 2016 8:12 PM
  • User-170056757 posted

    NullReferenceException was caught during the debug process on VS 2013; the root node is null  

    Saturday, November 5, 2016 9:34 PM
  • User753101303 posted

    Really ???

    This is where you are showing the error. You can configure VS to break on the line where it happens (my understanding is that it is on the "XmlNodeList nodelist = root.SelectNodes("//sign", nsmgr);"). Or the Console object would be null (ie you have an exception when reporting about an exception ???)

    Or just don't use try/catch as currently it does more harm than good:
    - you are writing the error details to the console. On a "real" server it would likely not show up anywhere.
    - you convert back an exception to a function result (and if you forgot to test the result you'll get an exception but your code will keep running possibly failing later and you'll likely waste some time to realize that the exception you get is caused by an earlier exception)

    Saturday, November 5, 2016 10:56 PM
  • User303363814 posted

    Set a breakpoint on the first line of the method.  Run the code.  When the breakpoint is hit examine the value of the 'doc' and 'id' parameters.  Post the values here.

    Sunday, November 6, 2016 5:44 AM
  • User-170056757 posted

    The value is Null and i did set two break points; the first one is on line 55 and line 133. I posted the code. The try catch error was placed to track the error  as building the code. I am new in C# and APis. help is appreciated 

    Sunday, November 6, 2016 6:18 PM
  • User303363814 posted

    Why did you show the previous code if you already knew that bad parameters were being passed????

    (BTW - how could it possibly be useful to tell us that you have breakpoints on lines 55 and 133 if you never show us any line numbers????)

    Your recent post tells us that 'inreposonsestream' is missing a root element.

    So .... what is the value of 'inresponsonsestream'?  Where does it come from?  How is it created?

    Debugging is a process of working back towards a root cause.  If you find a problem, determine what caused the problem.  Repeat until you get to the root cause.  Then investigate the root cause.

    Monday, November 7, 2016 12:06 AM
  • User-170056757 posted

    i came to realize the default response body type was Jason and I had to re-set the response body type to XML so it resolve this issue. No null values; I am currently trying to read the nodes using xpath and posting the values on the web form.  any tips or code snippet is appreciated  

    Monday, November 7, 2016 6:36 AM
  • User1724605321 posted

    Hi omega71044,

    I am currently trying to read the nodes using xpath and posting the values on the web form.  any tips or code snippet is appreciated  

    Please refer to Manipulate XML data with XPath and XmlDocument (C#) and Select Nodes Using XPath Navigation .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 7, 2016 7:19 AM
  • User303363814 posted

    Please, please , please - show us what response you get!  We cannot possibly help you interpret a random web api response.  Show us the data and we can help!  There has to be a specific question with specific data and specific code.

    Monday, November 7, 2016 8:42 AM
  • User-170056757 posted

    ok

    Wednesday, November 9, 2016 12:47 AM