asp.net - C# LibCurlNet , keeping the same session id in Cookie.txt -


i beginner c# developer , i'm bit stuck.

i have project need keep same session id cookie when making http requests. requests work fine, create need in api (its currency exchange api). problem when create payment buy lets 100 pounds go screen need authorise payment. in here have exchange rate valid 1.5 minute. when time ends rates refreshes. lock rates need authorise payment. when authorise go screen waiting payments. here rate locked problem rate not same locked. owned of website told me because session id not same on request create payment , authorise.

this api documentation:

api connection can established via httpsrequest components curl. if curl library not available in desired language version alternative should provide follows:

  1. all curl queries/requests/data transfer should use https protocol only.
  2. all sensitive information must sent post method;
  3. ssl certificate , ssl host verification. prevent dns hack attack.see curlopt_ssl_verifyhost & curlopt_ssl_verifypeer
  4. session , cookie support;

the example code found in php using curl. came using libcurlnet:

public class http {      string _xmlresponse = string.empty;       private static easy easy;      private static easy.writefunction wf = null;         public string performrequest(string xml, string querystring, string url)     {          try         {             _xmlresponse = string.empty;             curl.globalinit((int)curlinitflag.curl_global_all);             easy = new easy();              wf = new easy.writefunction(onwritedata);             easy.setopt(curloption.curlopt_url, url);             easy.setopt(curloption.curlopt_writefunction, wf);             easy.setopt(curloption.curlopt_cookiefile, httpcontext.current.request.mappath("~/cookies.txt"));             easy.setopt(curloption.curlopt_cookiejar, httpcontext.current.request.mappath("~/cookies.txt"));                easy.setopt(curloption.curlopt_ssl_verifypeer, true);             easy.setopt(curloption.curlopt_ssl_verifyhost, true);             easy.setopt(curloption.curlopt_cainfo, httpcontext.current.request.mappath("~/ssl.crt"));             easy.setopt(curloption.curlopt_header, 0);             easy.setopt(curloption.curlopt_followlocation, true);             easy.setopt(curloption.curlopt_useragent, "xxx web xml api");              easy.setopt(curloption.curlopt_buffersize, 65536);             easy.setopt(curloption.curlopt_postfields, cleanxml(xml));             easy.perform();             easy.cleanup();              curl.globalcleanup();         }         catch (exception ex)         {             return ex.message;         }         return _xmlresponse;     }      public int32 onwritedata(byte[] buf, int32 size, int32 nmemb,         object extradata)     {         _xmlresponse = _xmlresponse + system.text.encoding.utf8.getstring(buf);          return size * nmemb;     }      private string cleanxml(string xml)     {          char[] sep = environment.newline.toarray();         string[] lines = xml.split(sep);         stringbuilder sb = new stringbuilder();         foreach (string line in lines)         {             sb.append(line.trim());         }         return sb.tostring();     }  } 

the problem cookie.txt updates session id every connection make.

code httpwebrequest:

public class httpv2 {    static cookiecollection cookie = new cookiecollection();    string responsefromserver;    public string gethttpresponse(string url, string requestbody)    {           //webrequest request = webrequest.create(url);         httpwebrequest request = httpwebrequest.createhttp(url);          x509certificate cert = x509certificate.createfromcertfile(@"d:\www_root\xxxxxxxxxxx\xxxxx\ssl.crt");         request.clientcertificates.add(cert);         request.method = "post";          //httpcontext context = httpcontext.current;        request.cookiecontainer = new cookiecontainer();        request.useragent = "tranfermate web xml api";        if (cookie != null)        {             request.cookiecontainer.add(cookie);         }        byte[] bytearray = encoding.utf8.getbytes(requestbody);        request.contenttype = "application/x-www-form-urlencoded";        request.contentlength = bytearray.length;         stream datastream = request.getrequeststream();        datastream.write(bytearray, 0, bytearray.length);        datastream.close();         #region response.        using (webresponse response = request.getresponse())        {            if (requestbody.contains("<force_refresh>1</force_refresh>"))            {                cookie = ((httpwebresponse)response).cookies;            }            if (((httpwebresponse)response).statuscode == httpstatuscode.ok)                using (stream stream = response.getresponsestream())                {                    if (stream != null)                    {                        using (streamreader reader = new streamreader(stream))                        {                            responsefromserver = reader.readtoend();                             reader.close();                        }                        stream.close();                    }                }            response.close();        }        #endregion         return responsefromserver;    } } 

instead of using curl not use built-in httpwebrequest class instead? httpwebrequest has cookiecontainer property can assign cookiecontainer object. object can re-used between requests, think should after.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -