【问题标题】:Controlling the action of System.Diagnostics.Process.Start C#控制 System.Diagnostics.Process.Start C# 的操作
【发布时间】:2014-02-27 13:44:38
【问题描述】:

我在我的客户端应用程序(用 c# 编写)上使用 System.Diagnostics.Process.Start 函数来调用 URL 并执行相应的操作。

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

运行 URL 会在服务器上进行某些更改并给出 PLAIN_TEXT 响应。 现在的事情是调用这个函数,它会打开 Web 浏览器并显示 targetURL 已被调用。 有什么办法可以阻止这种情况吗?

我的意思是我希望更改发生,但浏览器不应该打开。 不过应该进行更改。

【问题讨论】:

  • 创建一个 ProcessStartInfo 对象并将其传递给进程,有一个选项。
  • 是http请求吗?
  • 你的意思是这样的吗? ProcessStartInfo sInfo = new ProcessStartInfo(targetURL); Process.Start(sInfo);
  • @Grzenio 是的,它是一个 http 请求
  • 我认为应该有更好的方法来实现你想要的。虽然我们并不完全知道你需要什么。 Process.Start(url) 将启动默认浏览器...我猜这有什么作用?但无论如何,如果你想在背后使用:ProcessStartInfo.CreateNoWindowProcessStartInfo.NoShellExec,但一个简单的 HttpRequest 也可以做到这一点,或许?

标签: c# system.diagnostics


【解决方案1】:

如果我正确理解您的问题,您实际上是想发出一个 http 请求并使用作为响应返回的文本。如果是这种情况,您应该使用HttpClient 而不是启动进程。

来自 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);

【讨论】:

    【解决方案2】:

    只需使用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 ();
    

    【讨论】:

      【解决方案3】:

      您可以使用HttpClient 类。 GetAsync 方法将执行 GET 请求。还有 POST、PUT 和 DELETE 方法。

      // Call the GetWorkflow web-method.
      using (HttpClient client = new HttpClient())
      {
          string myUrl = "http://someurl";
          var response = client.GetAsync(myUrl).Result;
      }
      

      您还可以查看WebClient 类(例如WebClient.DownloadString)或RestSharp。网上有很多这两种方法的例子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-23
        • 2017-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多