1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| public String curl_test()
{
string cookie = @"d:\api\cookie.file";
string cfecId;
string login;
string passwd;
string safeId;
string urlController;
cfecId = "1";
login = "log";
passwd = "pwdApi";
safeId = "1";
// certificats
string fichierca;
string fichierkey;
string fichiercli;
string Pwdcert;
//VNN
fichierca = "D:\\Api\\cert\\cecurwebVNN_cert.pem";
fichierkey = "D:\\Api\\cert\\cecurwebVNN_key.pem";
fichiercli = "D:\\Api\\cert\\cecurwebVNN_cli.pem";
Pwdcert = "pxdCert";
string urlparam = "cfecId=" + cfecId + "&login=" + login + "&password=" + passwd + "&safeid=" + safeId;
urlController = "https://monsite.com/controller/connect";
//connexion controller
Curl.GlobalCleanup();
Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
try
{
Easy easy_Exec = new Easy();
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData);
easy_Exec.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy_Exec.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf);
easy_Exec.SetOpt(CURLoption.CURLOPT_POST, true);
easy_Exec.SetOpt(CURLoption.CURLOPT_POSTFIELDS, urlparam);
easy_Exec.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false);
easy_Exec.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
easy_Exec.SetOpt(CURLoption.CURLOPT_SSLCERT, fichiercli);
easy_Exec.SetOpt(CURLoption.CURLOPT_SSLCERTPASSWD, Pwdcert);
easy_Exec.SetOpt(CURLoption.CURLOPT_SSLCERTTYPE, "PEM");
easy_Exec.SetOpt(CURLoption.CURLOPT_SSLKEY, fichierkey);
easy_Exec.SetOpt(CURLoption.CURLOPT_SSLKEYPASSWD, Pwdcert);
easy_Exec.SetOpt(CURLoption.CURLOPT_CAINFO, fichierca);
easy_Exec.SetOpt(CURLoption.CURLOPT_COOKIEJAR, cookie);
easy_Exec.SetOpt(CURLoption.CURLOPT_ENCODING, "");
easy_Exec.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
// For debugging will print received data to console.
Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
easy_Exec.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
easy_Exec.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
easy_Exec.SetOpt(CURLoption.CURLOPT_URL, urlController);
CURLcode resCurl = easy_Exec.Perform();
return resCurl.ToString();
}
catch
{
return "";
}
}
private Int32 OnHeaderData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
String currentHeader = System.Text.Encoding.UTF8.GetString(buf).Trim();
if (currentHeader.Contains(":"))
{
Char[] separator = new Char[] { ':' };
String[] split = currentHeader.Split(separator, 2);
if (!this.headers.ContainsKey(split[0]))
this.headers.Add(split[0].Trim(), split[1].Trim());
}
return size * nmemb;
}
private Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
this.stream.Write(buf, 0, size * nmemb);
return size * nmemb;
}
public static void OnDebug(CURLINFOTYPE infoType, String msg, Object extraData)
{
// print out received data only
Console.WriteLine(msg);
if (infoType == CURLINFOTYPE.CURLINFO_DATA_IN)
Console.WriteLine(msg);
} |