简体   繁体   English

控制System.Diagnostics.Process.Start C#的操作

[英]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. 我在我的客户端应用程序(用c#编写)上使用System.Diagnostics.Process.Start函数来调用URL并执行相应的操作。

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

Running the URL makes certain changes on the server and gives a PLAIN_TEXT response. 运行URL会在服务器上进行某些更改并提供PLAIN_TEXT响应。 Now the thing is on calling this function it opens the web browser and shows that the targetURL has been called. 现在问题在于调用此函数它打开Web浏览器并显示已调用targetURL 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. 但应该做出改变。

If I understand your question correctly, you actually want to make a http request and use the text that was returned as a response. 如果我正确理解您的问题,您实际上想要发出http请求并使用作为响应返回的文本。 If this is the case, you should use HttpClient instead of starting a process. 如果是这种情况,您应该使用HttpClient而不是启动进程。

Example from MSDN: 来自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);

Just use a WebRequest : 只需使用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 ();

You could use the HttpClient class. 您可以使用HttpClient类。 The GetAsync method will perform a GET request. GetAsync方法将执行GET请求。 There are also methods for POST, PUT, and DELETE. 还有POST,PUT和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 (eg WebClient.DownloadString ), or RestSharp . 您还可以查看WebClient类(例如WebClient.DownloadString )或RestSharp Plenty of examples of both of these on the net. 网上有很多这两个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM