在C#上重复PHP Curl请求
我尝试使用C#上的Curl向服务器发送请求。
我有php功能,效果很好!
function sendRequest()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://whois.ripn.net");
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "ya.ru\r\n");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
我尝试在C#中重复它,但没有任何作用。
class curl {
private static Easy easy;
private static string SockBuff;
public void CurlInit()
{
Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
}
public void req()
{
easy = new Easy();
SockBuff = "";
try
{
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
easy.SetOpt(CURLoption.CURLOPT_URL, "http://whois.ripn.net");
easy.SetOpt(CURLoption.CURLOPT_PORT, "43");
easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "10");
easy.SetOpt(CURLoption.CURLOPT_CUSTOMREQUEST, "ya.ru\r\n");
easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy.Perform();
easy.Cleanup();
}
catch
{
}
Console.Write(SockBuff);
}
public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
// Console.Write(System.Text.Encoding.UTF8.GetString(buf));
SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
return size * nmemb;
}
}
为什么我的C#代码不起作用?