Search code examples
c#system.diagnostics

Controlling the action of System.Diagnostics.Process.Start C#


I am using the System.Diagnostics.Process.Start function on my client application (written in c#) to call a URL and perform the corresponding action.

string targetURL = "someurl";
System.Diagnostics.Process.Start(targetURL);

Running the URL makes certain changes on the server and gives a PLAIN_TEXT response. Now the thing is on calling this function it opens the web browser and shows that the targetURL has been called. Is there any way this could be stopped?

What I mean is that I want the changes to take place but the browser shouldn't open. The changes should be made though.


Solution

  • Just use a WebRequest:

    WebRequest request = WebRequest.Create ("someurl");
    // If required by the server, set the credentials.
    request.Credentials = CredentialCache.DefaultCredentials;
    // Get the response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
    // Get the stream containing content returned by the server.
    Stream dataStream = response.GetResponseStream ();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader (dataStream);
    // Read the content. 
    string responseFromServer = reader.ReadToEnd ();