2012-03-14 99 views
0

我的目标是读取CURL.exe文件的响应,该文件在提供必要参数时返回JSON字符串。阅读来自CURL.exe程序的响应

例:

curl -u admin:admin http://localhost:8080/jira/rest/api/2.0.alpha1/issue/PROJ-1 

上面的代码retuns我JSON格式如下回应:

{ 
    "self": "http://localhost:8080/rest/api/2/issue/10000/worklog/10000", 
    "author": { 
     "self": "http://localhost:8080/rest/api/2/user?username=admin", 
     "name": "admin", 
     "emailAddress": "[email protected]", 
     "avatarUrls": { 
      "16x16": "http://localhost:8080/secure/useravatar?size=small&avatarId=10122", 
      "48x48": "http://localhost:8080/secure/useravatar?avatarId=10122" 
     }, 
     "displayName": "Vamshi Vanga", 
     "active": true 
    }, 
    "updateAuthor": { 
     "self": "http://localhost:8080/rest/api/2/user?username=admin", 
     "name": "admin", 
     "emailAddress": "[email protected]", 
     "avatarUrls": { 
      "16x16": "http://localhost:8080/secure/useravatar?size=small&avatarId=10122", 
      "48x48": "http://localhost:8080/secure/useravatar?avatarId=10122" 
     }, 
     "displayName": "Vamshi Vanga", 
     "active": true 
    }, 
    "comment": "Read the articles and found some plugins to work with.", 
    "created": "2012-03-13T14:45:15.816+0530", 
    "updated": "2012-03-13T14:45:15.816+0530", 
    "started": "2012-03-13T14:44:00.000+0530", 
    "timeSpent": "1h", 
    "timeSpentSeconds": 3600, 
    "id": "10000" 
} 

我已经实现了这个代码,以获取详细信息:

Process myProcess = new Process(); 
ProcessStartInfo startInfo = new ProcessStartInfo("curl.exe -u admin:admin123 http://localhost:8080/jira/rest/api/2.0.alpha1/issue/PROJ-1");   
startInfo.UseShellExecute = false; 
startInfo.RedirectStandardOutput = true; 
myProcess.StartInfo = startInfo; 
startInfo.CreateNoWindow = true; 
startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
myProcess.Start(); 
myProcess.WaitForExit(); 
StreamReader myStreamReader = myProcess.StandardOutput; 
string myString = myStreamReader.ReadLine(); 
Console.WriteLine("JSON Response" +myString); 
myProcess.Close(); 
Console.ReadLine(); 

当上面的代码运行,它不会在代码提示中给我任何回应。当我在命令提示符下手动运行时,该命令运行正常。

+3

你为什么要使用外部程序呢? .NET已经有能力调用Http资源。使用[WebClient](http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx)或[HttpWebRequest](http://msdn.microsoft.com/en-us/library/ system.net.httpwebrequest.aspx)。 – 2012-03-14 16:43:37

+1

[WebClient] +1(http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx) – dtb 2012-03-14 16:48:09

+0

'myStreamReader.ReadToEnd();'not'myStreamReader.ReadLine();' – leppie 2012-04-30 04:04:10

回答

0

虽然你应该使用WebClientHttpWebRequest,这里是什么可能是错在你当前的代码......

的构造被用于的ProcessStartInfo不正确。你应该得到一个例外。你以某种方式吞咽吗?

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Full\Path\To\curl.exe", 
"-u admin:admin123 http://localhost:8080/jira/rest/api/2.0.alpha1/issue/PROJ-1"); 

而且你应该把等待时间命令teminate

myProcess.WaitForExit(5000); // Wait at most 5 seconds 
1

您可以使用此示例:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.IO; 

namespace CuRLTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // 
      // Setup the process with the ProcessStartInfo class. 
      // 
      ProcessStartInfo start = new ProcessStartInfo(); 
      start.FileName = @"C:\curl.exe"; // Specify exe name. 
      start.Arguments = "http://curl.haxx.se"; 
      start.UseShellExecute = false; 
      start.RedirectStandardOutput = true; 
      // 
      // Start the process. 
      // 
      using (Process process = Process.Start(start)) 
      { 
       // 
       // Read in all the text from the process with the StreamReader. 
       // 
       using (StreamReader reader = process.StandardOutput) 
       { 
        string result = reader.ReadToEnd(); 
        Console.Write(result); 
        Console.ReadLine(); 
       } 
      } 

     } 
    } 
}