How to request a string to a website using get method?

I want to send information along with the URL as a query string to a website. This i call

Process.Start("http://domain.com?value=anything");

but I don’t want to open the browser because I don’t want to interrupt the user who uses the software.

I have googled for this, but I found the code HttpRequest, but the problem is that this program does not add the query string. They just return me the html text of the webpage.

I want to get this information for storage in a database. Please advise me in this case.

+4
source share
2 answers

Here is what I use to execute the GET:

public T Get<T>(string url, IEnumerable<KeyValuePair<string, string>> data)
{
    var webApiUri = ConfigurationManager.AppSettings["WebApiUri"];

    var client = new HttpClient();

    try
    {
        string queryString = string.Empty;

        if (data != null)
        {
            queryString = data.Distinct().Select(x => string.Format("{0}={1}", x.Key, x.Value)).ToDelimitedString("&");
        }

        var response = client.GetAsync(string.Format("{0}/{1}?{2}", webApiUri, url, queryString)).Result;
        var responseContent = response.Content.ReadAsStringAsync().Result;
        return JsonConvert.DeserializeObject<T>(responseContent);
    }
    catch (Exception ex)
    {
        Debug.Assert(false, ex.Message);
        throw;
    }
}

T - . JSON, . client GET, . , JSON.

+1

WebClient.DownloadString, URL-, :

var x = WebClient.DownloadString("http://domain.com?value=anything");
+4

All Articles