我的目標是讀取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