我的目标是读取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();
当上面的代码运行,它不会在代码提示中给我任何回应。当我在命令提示符下手动运行时,该命令运行正常。
你为什么要使用外部程序呢? .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
[WebClient] +1(http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx) – dtb 2012-03-14 16:48:09
'myStreamReader.ReadToEnd();'not'myStreamReader.ReadLine();' – leppie 2012-04-30 04:04:10