C#LibCurlNet,在Cookie.txt中保持相同的会话ID

时间:2013-09-04 10:20:54

标签: c# asp.net

我是初学者C#开发者,我有点卡住了。

我有一个项目,我需要在发出HTTP请求时保留相同的会话ID cookie。 请求工作正常,我创建了我需要的API(它的货币兑换API)。问题是,当我创建一个付款购买让我说100磅我去一个我需要授权付款的屏幕。在这里,我的汇率有效期为1.5分钟。当时间结束时,费率会刷新。要锁定我需要授权付款的费率。当我授权我去屏幕等待付款。这里的费率现在已被锁定,但问题是费率与我锁定的不同。该网站的所有者告诉我,这是因为会话ID在创建付款和授权的请求上不一样。

这来自API文档:

  

可以通过HttpsRequest组件建立API连接   卷曲。如果cURL库没有所需的语言版本   然后替代方案应提供如下:

     
      
  1. 所有cURL查询/请求/数据传输都应仅使用HTTPS协议。
  2.   
  3. 所有敏感信息必须通过POST方式发送;
  4.   
  5. SSL证书和SSL主机验证。它将防止DNS黑客攻击。参见CURLOPT_SSL_VERIFYHOST& CURLOPT_SSL_VERIFYPEER
  6.   
  7. 会话和Cookie支持;
  8.   

我发现的示例代码是使用Curl的PHP。这就是我使用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();
    }

}

问题是cookie.txt会使用我建立的每个连接更新会话ID。

使用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 Get the 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;
   }
}

1 个答案:

答案 0 :(得分:0)

您可以不使用内置的HttpWebRequest类而不是使用Curl吗? HttpWebRequest具有CookieContainer属性,您可以为其分配CookieContainer个对象。这个对象可以在请求之间重用,所以我认为它应该做你想要的。