2012-03-15 27 views
0

我在C#中調用CURL來檢索數據。如何從CURL.exe中檢索數據?

以下是我的代碼:

Process p = new Process(); 
p.StartInfo.WorkingDirectory = @"D:\"; 
p.StartInfo.FileName = @"D:\curl.exe"; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.Arguments = "-u agentemail:password -k https://fantasia.zendesk.com/api/v1/tickets/1.xml"; 
p.Start(); 
p.WaitForExit(); 

但問題是捲曲獲取URL數據之後,我該如何取回來自卷邊的數據? 是否有一個命令將數據拉入如下的字符串中?

string x = p.OutputDataReceived(); 

p.OutputDataReceived(string x); 

非常感謝你。

+0

[捕捉與C#NSLOOKUP殼輸出(可能重複http://stackoverflow.com/questions/353601/capturing-nslookup-shell -out-with-c-sharp) – RedFilter 2012-03-15 11:04:12

+0

您是否考慮過使用[WebClient](http://msdn.microsoft.com/zh-cn/library/system.net.webclient.aspx)或[HttpWebRequest](http: /msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx)而不是執行外部進程來發出HTTP請求? – dtb 2012-03-15 11:06:00

+0

[讀取來自CURL.exe程序的響應]的可能的重複(http://stackoverflow.com/questions/9706162/read-response-from-curl-exe-program) – dtb 2012-03-15 11:06:57

回答

0

你可以添加這些行:

ProcessStartInfo start = new ProcessStartInfo(); 
start.FileName = @"D:\curl.exe"; // Specify exe name. 
start.Arguments = "-u agentemail:password -k https://fantasia.zendesk.com/api/v1/tickets/1.xml"; 
start.UseShellExecute = false; 
start.RedirectStandardOutput = true; 

// Start the process. 
using (Process p = Process.Start(start)) { 
    // Read in all the text from the process with the StreamReader 
    using (StreamReader reader = p.StandardOutput) { 
     string result = reader.ReadToEnd(); 
     Console.Write(result); 
    } 
} 
1
ProcessStartInfo start = new ProcessStartInfo(); 

start.FileName = @"C:\curl.exe"; // Specify exe name. 

start.Arguments = "-i -X POST -H \"Content-Type: text/xml\" -u \"curl:D6i\" --insecure --data-binary @" + cestaXmlUsers + " \"https://xxx.sk/users-import\""; 

start.UseShellExecute = false; 

start.RedirectStandardOutput = true; 

Process p = Process.Start(start); 

string result = p.StandardOutput.ReadToEnd(); 

p.WaitForExit();