public Curl(int bufferSize) { if (_initialized != CurlCode.Ok) { if (_initialized == CurlCode.NotInitialized) { throw new CurlNotInitializedException(); } throw new CurlException(_initialized, this); } _buffer = new byte[bufferSize]; _errorBuffer = Marshal.AllocHGlobal(ErrorBufferSize); _curl = CurlNative.EasyInit(); if (_curl == IntPtr.Zero) { throw new CurlEasyInitializeException("Curl Easy failed to initialize!"); } CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Errorbuffer, _errorBuffer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } }
public ArraySegment <byte> GetBytes(string url) { IntPtr urlpointer = IntPtr.Zero; IntPtr useragentpointer = IntPtr.Zero; try { urlpointer = MarshalString.StringToUtf8(url); CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Url, urlpointer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } useragentpointer = MarshalString.StringToUtf8(UserAgent); CurlCode result1 = CurlNative.EasySetOpt(_curl, CurlOption.Useragent, useragentpointer); if (result1 != CurlCode.Ok) { throw new CurlException(result1, this); } CurlCode result2 = CurlNative.EasySetOpt(_curl, CurlOption.WriteData, this); if (result2 != CurlCode.Ok) { throw new CurlException(result2, this); } CurlCode result3 = CurlNative.EasySetOpt(_curl, CurlOption.WriteFunction, WriteCallback); if (result3 != CurlCode.Ok) { throw new CurlException(result3, this); } CurlCode result4 = CurlNative.EasyPerform(_curl); if (result4 != CurlCode.Ok) { throw new CurlException(result4, this); } return(new ArraySegment <byte>(_buffer, 0, _offset)); } finally { if (urlpointer != IntPtr.Zero) { Marshal.FreeHGlobal(urlpointer); } if (useragentpointer != IntPtr.Zero) { Marshal.FreeHGlobal(useragentpointer); } } }
public void Reset() { _offset = 0; CurlNative.EasyReset(_curl); CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Errorbuffer, _errorBuffer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } }
public string Post(string url, string data, Encoding encoding) { IntPtr urlpointer = IntPtr.Zero; IntPtr useragentpointer = IntPtr.Zero; IntPtr datapointer = IntPtr.Zero; try { urlpointer = MarshalString.StringToUtf8(url); CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Url, urlpointer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } useragentpointer = MarshalString.StringToUtf8(UserAgent); CurlCode result1 = CurlNative.EasySetOpt(_curl, CurlOption.Useragent, useragentpointer); if (result1 != CurlCode.Ok) { throw new CurlException(result1, this); } datapointer = MarshalString.StringToUtf8(data); CurlCode result2 = CurlNative.EasySetOpt(_curl, CurlOption.Postfields, datapointer); if (result2 != CurlCode.Ok) { throw new CurlException(result2, this); } CurlCode result3 = CurlNative.EasySetOpt(_curl, CurlOption.WriteData, this); if (result3 != CurlCode.Ok) { throw new CurlException(result3, this); } CurlCode result4 = CurlNative.EasySetOpt(_curl, CurlOption.WriteFunction, WriteCallback); if (result4 != CurlCode.Ok) { throw new CurlException(result4, this); } CurlCode result5 = CurlNative.EasyPerform(_curl); if (result5 != CurlCode.Ok) { throw new CurlException(result5, this); } return(encoding.GetString(_buffer, 0, _offset)); } finally { if (urlpointer != IntPtr.Zero) { Marshal.FreeHGlobal(urlpointer); } if (useragentpointer != IntPtr.Zero) { Marshal.FreeHGlobal(useragentpointer); } if (datapointer != IntPtr.Zero) { Marshal.FreeHGlobal(datapointer); } } }