C#控制台表单输入问题

时间:2019-01-21 13:12:43

标签: c# forms api input console

我正在用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";
    }

}

}

1 个答案:

答案 0 :(得分: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 + "";

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

它应该工作,因为您声明了仅设置一次的字段网址。而且我建议您将其转换为每次都要计算的属性,以便它尊重对该静态字段的更改。实际上类似于声明方法

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