从CURL.exe程序读取响应

时间:2012-03-14 16:39:34

标签: c#

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

例如:

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

上面的代码以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": "admin@admin.com",
        "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": "admin@admin.com",
        "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();

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

2 个答案:

答案 0 :(得分: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();
                }
            }

        }
    }
}

答案 1 :(得分: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");

此外,您应该等待命令终止

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