从CURL.exe程序读取响应
本文关键字:读取 响应 程序 exe CURL | 更新日期: 2023-09-27 18:27:52
我的目标是从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();
当上面的代码运行时,它不会通过代码在提示中给我任何响应。但是,当我在命令提示符下手动运行时,该命令运行良好。
您可以使用此示例:
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();
}
}
}
}
}
虽然您应该使用WebClient或HttpWebRequest,但以下是您当前代码中可能出现的错误。。。
用于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