Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将一段PHP cURL代码翻译为C_C#_Php_Curl - Fatal编程技术网

C# 将一段PHP cURL代码翻译为C

C# 将一段PHP cURL代码翻译为C,c#,php,curl,C#,Php,Curl,我试图翻译这段代码,它是为了从basis的网站“活动跟踪器”获取数据而编写的。我想在这里完成的任务是获取Http响应头中返回的访问令牌。原始PHP代码没有问题,但是,有几个cUrl选项我不知道如何映射到C的WebClient实现。这些选项包括CURLOPT_RETURNTRANSFER、CURLOPT_FOLLOWLOCATION和CURLOPT_COOKIESESSION 下面列出了详细问题,下面是一段PhP代码: $login_data = array( 'usern

我试图翻译这段代码,它是为了从basis的网站“活动跟踪器”获取数据而编写的。我想在这里完成的任务是获取Http响应头中返回的访问令牌。原始PHP代码没有问题,但是,有几个cUrl选项我不知道如何映射到C的WebClient实现。这些选项包括CURLOPT_RETURNTRANSFER、CURLOPT_FOLLOWLOCATION和CURLOPT_COOKIESESSION

下面列出了详细问题,下面是一段PhP代码:

    $login_data = array(
        'username' => $this->username,
        'password' => $this->password,
    );

    // Initialize the cURL resource and make login request
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => 'https://app.mybasis.com/login',
        CURLOPT_RETURNTRANSFER => false, //default: true
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $login_data,
        CURLOPT_FOLLOWLOCATION => true, // defulat: true
        CURLOPT_HEADER => 1,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_COOKIEJAR => $this->cookie_jar
    ));
使用此代码,结果将是:其中包含我需要的访问令牌

HTTP/1.1 100 Continue

HTTP/1.1 302 Found
Date: Sun, 22 Jun 2014 02:03:47 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: keep-alive
Server: TornadoServer/3.2
Location: https://app.mybasis.com
Cache-Control: max-age=0
Set-Cookie: access_token=f0659120d52f3fde9edfw1d908b81f0f; Domain=.    mybasis.com; expires=Sun, 01 Jan 2040 00:00:00 GMT; Path=/
Set-Cookie: scope=login; Domain=.mybasis.com; expires=Sun, 01 Jan     2040 00:00:00 GMT; Path=/
Set-Cookie: refresh_token=982ff518bfe114e2c03f360f0dfbfke1; Domain=.    mybasis.com; expires=Sun, 01 Jan 2040 00:00:00 GMT; Path=/

HTTP/1.1 200 OK
Server: nginx/1.4.3
Date: Sun, 22 Jun 2014 02:03:47 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5619
Last-Modified: Fri, 25 Apr 2014 04:42:49 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5359e7c9-15f3"
Accept-Ranges: bytes
但是,当我使用C WebClient与basis通信时,如下所示:

 var data = new NameValueCollection
 {
     { "username", username},
     { "password", password},
 };

 HttpWebResponse response = client.UploadValues("https://app.mybasis.com/login", "POST", data);

 foreach (string name in client.ResponseHeaders.Keys)
 {
     Console.WriteLine(name+"="+client.ResponseHeaders[name]);
 }
我只能得到这个:

Connection=keep-alive
Vary=Accept-Encoding
Accept-Ranges=bytes
Content-Length=5619
Content-Type=text/html; charset=utf-8
Date=Sun, 22 Jun 2014 02:17:44 GMT
ETag="53f3e7c9-99f3"
Last-Modified=Fri, 25 Apr 2014 04:42:49 GMT
Server=nginx/1.4.3
作为回应


有人知道为什么我不能得到前两个Http响应,而只能得到第三个响应吗

为了从您正在寻找的C中获得功能,您需要在.NET framework中使用Http web请求API。它应该提供您所需要的功能。

我也做了同样的事情,但使用了4.5 HttpClient

        CookieContainer cookies = new CookieContainer();
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;

        HttpClient client = new HttpClient(handler);


        // Create the HttpContent for the form to be posted.
        var requestContent = new FormUrlEncodedContent(new[] {
            new KeyValuePair<string, string>("username", "***"),
            new KeyValuePair<string, string>("password", "***"),
        });

        // Get the response.
        HttpResponseMessage response = await client.PostAsync(
            "https://app.mybasis.com/login",
            requestContent);

        // Get the response content.
        HttpContent responseContent = response.Content;


        Uri uri = new Uri("https://app.mybasis.com/login");
        IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
        foreach (Cookie cookie in responseCookies)
            Console.WriteLine(cookie.Name + ": " + cookie.Value);

是的,我确实使用了HttpWebRequestAPI,但是,我的问题是我找不到将CURLOPT_RETURNTRANSFER、CURLOPT_FOLLOWLOCATION和CURLOPT_COOKIESESSION转换为.NET的选项。C不是PHP,所以试着换一种方式来考虑。我相信MS提供的HttpWebRequest库已经返回了数据,而您不需要询问这些数据,还有下面的位置头等等。至于cookie会话,你可以参考这篇文章:检查这个重复的问题