Reputation: 125
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.
Upvotes: 2
Views: 1583
Reputation: 36679
If I understand your question correctly, you actually want to make a http request and use the text that was returned as a response. If this is the case, you should use HttpClient instead of starting a process.
Example from MSDN:
// Create a New HttpClient object.
HttpClient client = new HttpClient();
// Call asynchronous network methods in a try/catch block to handle exceptions
try
{
HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
}
// Need to call dispose on the HttpClient object
// when done using it, so the app doesn't leak resources
client.Dispose(true);
Upvotes: 5
Reputation: 152634
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 ();
Upvotes: 4
Reputation: 37222
You could use the HttpClient class. The GetAsync method will perform a GET request. There are also methods for POST, PUT, and DELETE.
// Call the GetWorkflow web-method.
using (HttpClient client = new HttpClient())
{
string myUrl = "http://someurl";
var response = client.GetAsync(myUrl).Result;
}
You can also look into the WebClient class (e.g. WebClient.DownloadString), or RestSharp. Plenty of examples of both of these on the net.
Upvotes: 2