【问题标题】:C# Console Form INPUT issue [closed]C# 控制台表单输入问题 [关闭]
【发布时间】:2019-01-21 13:12:42
【问题描述】:

我正在使用 API (https://github.com/Hipo/university-domains-list-api) 编写小控制台程序,但我遇到了一个问题 - 当我第一次将信息写入变量 inputkeyword 和 inputcountry 时,它工作正常,但第二次等等时我得到与我第一次尝试相同的答案。感谢您的帮助。

    public interface IRequestHandler
    {
        //Method to get the data of the repo provided by the url
        string GetResult(string url);
    }
    // using request handler to get an url address
    public static string GetResult(IRequestHandler requestHandler)
    {
        return requestHandler.GetResult(RequestConstants.Url);
    }

    public class RestSharpRequestHandler : IRequestHandler
    {
        public string GetResult(string url)
        {
            var client = new RestClient(url);
            var response = client.Execute(new RestRequest());
            return response.Content;
        }
    }

    public class Input
    {
        public static string userInputKeyWord;
        public static string userInputCountry;
        public static void dot()
        {
            Console.WriteLine("Insert keyword which you need to find your university");
            userInputKeyWord = Console.ReadLine();
            Console.WriteLine("You entered keyword {0}", userInputKeyWord);
            Console.WriteLine();


            Console.WriteLine("Insert country which you need");
            userInputCountry = Console.ReadLine();
            Console.WriteLine("You entered country {0}", userInputCountry);
            Console.WriteLine();

            Console.WriteLine("Creating the list of schools according to your request.");
            Console.WriteLine();
            IRequestHandler restSharpRequestHandler = new RestSharpRequestHandler();
            var response = GetResult(restSharpRequestHandler);
            var Results = JsonConvert.DeserializeObject<List<School>>(response);
            if (Results.Count() == 0)
            {
                Console.WriteLine("Nothing found.");
            }
            else
            {
                foreach (var release in Results)
                {
                    Console.WriteLine("Country: {0}", release.Country);
                    Console.WriteLine("Name: {0}", release.Name);
                    Console.WriteLine("Domains: {0}", release.Domains);
                    Console.WriteLine();

                }
            }
        }
    }

    // JSON properties
    public class School
    {
        [JsonProperty("web_pages")]
        public Uri[] WebPages { get; set; }

        [JsonProperty("alpha_two_code")]
        public string AlphaTwoCode { get; set; }

        [JsonProperty("state-province")]
        public object StateProvince { get; set; }

        [JsonProperty("country")]
        public string Country { get; set; }

        [JsonProperty("domains")]
        public string[] Domains { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }
    }

    //url for getting information from json
    public class RequestConstants : Input
    {

        //public static string userInputKeyWord;
        //public static string userInputCountry;
        public string BaseUrl = "http://universities.hipolabs.com/";
        public static string Url = "http://universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";
        public string UserAgent = "User-Agent";
        public string UserAgentValue = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
    }

}

}

【问题讨论】:

  • 这是您希望我们为您调试的大量代码。这是开始熟悉调试的好时机。使用调试器,您可以在任何代码行上放置一个断点,以暂停该行的运行时执行。然后,您可以在代码执行时逐行逐步执行代码,并观察确切的运行时行为和变量值的变化。当你这样做时,你首先在哪一行观察到一个意想不到的结果?当时的价值观是什么?观察到的结果是什么?你期待什么结果?为什么?
  • 观察到的结果是,每次我写关键字和国家时,我都会得到不同的结果——根据我的查询(url)列出的国家、名称和域。现在即使第二次尝试写关键字和国家,我也会得到相同的结果。
  • 您错过了有关使用调试器和识别特定代码行和特定变量值的部分。这才是真正重要的部分。
  • 我在公共静态字符串 Url = "universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";如果我不使用静态,我会在其他地方收到错误 return requestHandler.GetResult(RequestConstants.Url);
  • 好吧,那行显示的代码甚至不应该编译。这些变量未在该范围内定义。至于您的问题,在这种情况下,“大多数问题”是什么? Url 的结果值是多少?您期望该值是多少?为什么?

标签: c# forms api input console


【解决方案1】:

换行

public static string Url = "http://universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";

public static string Url => "http://universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";

(注意“=”已更改为箭头“=>”)

它应该可以工作,因为您声明了仅设置一次的字段 Url。我建议你把它变成一个每次都会计算的属性,这样它就会尊重静态字段的变化。它实际上类似于声明一个方法

public static string GetUrl() {
    return "http://universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";
}

此外,最好使用 RestSharp API 向请求添加参数。我会更新你的代码如下

public interface IRequestHandler
{
    //Method to get the data of the repo provided by the url
    string GetResult(string keyword, string country);
}
// using request handler to get an url address
public static string GetResult(IRequestHandler requestHandler, string keyword, string country)
{
    return requestHandler.GetResult(keyword, country);
}

public class RestSharpRequestHandler : IRequestHandler
{
    const string url = "http://universities.hipolabs.com/search";

    public string GetResult(string keyword, string country)
    {
        //"http://universities.hipolabs.com/search?name=" + userInputKeyWord + "&country=" + userInputCountry + "";
        //the values given in the url are GET params, so we add it using RestSharp API
        //also it's unsafe to manually concatenate parameters in url because some values should be encoded according to HTTP specification. Let RestSharp does it for you
        var client = new RestClient(url);
        var request = new RestRequest(Method.GET);
        request.AddParameter("name", keyword);
        request.AddParameter("country", country);
        var response = client.Execute(request);
        return response.Content;
    }
}

和线

var response = GetResult(restSharpRequestHandler);

会变成

var response = GetResult(restSharpRequestHandler, userInputKeyWord, userInputCountry);

【讨论】:

  • 我已经更新了我的帖子。希望它能帮助您改进代码。
  • 我看到了,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 2017-01-24
  • 2018-01-16
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多