4

I want to send an information along with the url like a query string to the web site. This is exactly I call

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

but I don't want to open browser because don't want to interrupt the user who is using the software.

I have googled for this but I found some HttpRequest code but the problem is this those program is not appending the query string. They are just returning me the html text of the web page.

I want to retrieve that information to store in database. Please guide me in this case.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
RaviKant Hudda
  • 1,042
  • 10
  • 25

2 Answers2

4

Use WebClient.DownloadString to get a page from a specific url, without using browsers:

var x = WebClient.DownloadString("http://domain.com?value=anything");
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Here is something I use to perform a 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 is the return type of the method. The response is expected as JSON, but you don't have to worry about that part. It's how the client performs a GET request that you're interested in. This is just example based around a JSON result.

Callum Linington
  • 14,213
  • 12
  • 75
  • 154
  • See edits. Its just an example of how to perform a GET request to website with some information. I'm trying to help the OP and this is what I understand him to be asking. – Callum Linington Sep 24 '14 at 14:46