WebRequest正在调用localhost

时间:2017-02-17 16:11:32

标签: c# xml asp.net-web-api httpwebrequest webrequest

我正在尝试调用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

请注意,它声称请求的网址是http://localhost:62324/,而不是我在代码中指定的URI。我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

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

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