Search code examples
c#xmlasp.net-web-apihttpwebrequestwebrequest

WebRequest is calling localhost


I am attempting to call a RESTful API but for some reason, whenever I make the call I seem to be calling localhost instead of the specified URI.

Here is the code I am working with:

using System.IO;
using System.Net;

namespace WebApi.Models
{
    public class GET
    {
        public static void Main()
        {
            /* The XML Request */
            string xmlRequest = @"
            <request>        
                <auth>
                    <type>basic</type>
                    <username>USERNAME</username>
                    <password>PASSWORD</password>
                </auth>
                <method>
                    <name>getProperties</name>
                    <params>
                        <propertyIds>356930</propertyIds>
                        <showAllStatus>0</showAllStatus>
                    </params>
                </method>
            </request>";

            /* Initiate a Web Request object */
            WebRequest request = WebRequest.Create ("https://ach.entrata.com/api/properties");
            request.Method = "GET";

            /* Initiate the request writer */
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());

            /* If you want to send an XML Request, use these options */
            request.ContentType = "APPLICATION/XML; CHARSET=UTF-8";
            requestWriter.Write(xmlRequest);

            requestWriter.Close();

            /* Read the response */
            StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            responseReader.Close();
        }
    }
}

And here is a screenshot of the response: Error Message

Notice it claims that the requested URL is http://localhost:62324/ and not the URI I specified in my code. Am I missing something obvious here?


Solution

  • I figured it out. I'll paste the fixed code here in case anyone else runs into this problem in the future. The main error was that I was trying to run the code as a web application when it needed to be a console application. Additionally, I was trying to run a GET call but it requires a POST call in order to send the query arguments to the server. After that I had another small error and I fixed it by moving the @" onto the next line to eliminate any leading spaces. Once that was done my code ran fine and returned the information to the console window perfectly.

    using System;
    using System.IO;
    using System.Net;
    
    namespace Console_API_Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                /* The XML Request */
                string xmlRequest = 
              @"<request>
    
    
                <auth>
    
                    <type> basic </type>
    
                    <username> USERNAME </username>
    
                    <password> PASSWORD </password>
    
                </auth>
    
                <method>
    
                    <name> getProperties </name>
    
                    <params>
    
                         <propertyIds> 356930 </propertyIds>
    
                        <showAllStatus> 0 </showAllStatus>
    
                    </params>
    
                  </method>
    
            </request>";
    
    
            /* Initiate a Web Request object */
            WebRequest request = WebRequest.Create("https://ach.entrata.com/api/properties");   
            request.Method = "POST";
    
            /* Initiate the request writer */
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
    
            /* If you want to send an XML Request, use these options */
            request.ContentType = "APPLICATION/XML; CHARSET=UTF-8";                             
            requestWriter.Write(xmlRequest);
    
            requestWriter.Close();
    
            /* Read the response */
            StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            responseReader.Close();
            Console.WriteLine(responseData);
        }
    }
    }