WebRequest 正在调用本地主机

WebRequest is calling localhost

我正在尝试调用 RESTful API 但出于某种原因,每当我进行调用时,我似乎都在调用 localhost 而不是指定的 URI。

这是我正在使用的代码:

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();
        }
    }
}

这是响应的屏幕截图: Error Message

请注意,它声称请求的 URL 是 http://localhost:62324/ 而不是我在代码中指定的 URI。我在这里遗漏了什么明显的东西吗?

我明白了。我将在此处粘贴固定代码,以防将来 运行 其他人遇到此问题。主要错误是我试图 运行 代码作为 Web 应用程序,而它需要成为控制台应用程序。此外,我试图 运行 一个 GET 调用,但它需要一个 POST 调用才能将查询参数发送到服务器。之后我又遇到了另一个小错误,我通过将 @" 移到下一行以消除任何前导空格来修复它。完成后我的代码 运行 正常并将信息返回到控制台 window完美。

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);
    }
}
}