C代码:cURL POST

时间:2014-12-25 17:44:44

标签: c post curl libcurl

我的代码很难过。我正在尝试使用cURL连接到网站。 我们来看看网站的代码:

<td class="input "><input tabindex="1" class="text" name="authentificationLogin" id="authentificationLogin" type="text"/></td>
<td class="input "><input tabindex="2" class="text" autocapitalize="off" autocorrect="off" name="authentificationPassword" id="authentificationPassword" type="password"/></td>
<td class="input "><input name="authentificationRedirection" id="authentificationRedirection" type="hidden" value="http://myWebsite/?Redirect"/><input name="authentification08e11696a1" id="authentification08e11696a1" type="hidden" value=""/><br/><button class="button button-style" type="submit" name="Submit" id="authentificationSubmit" onclick="return lock(&quot;id:authentificationSubmit&quot;);">

现在,让我们看看我的代码:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <curl/easy.h>

void Demarrage(void){
    CURLcode res;
    const char *userAgentMozilla = "Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20";     // set userAgent
    const char *data = "&to=gaia&login=myName&password=myPassword&isBoxStyle=";     // set the data to send
    curl_global_init(CURL_GLOBAL_ALL);
    CURL *handle;
    handle = curl_easy_init();

    curl_easy_setopt(handle,CURLOPT_COOKIESESSION,1);       // clean up session.
    curl_easy_setopt(handle,CURLOPT_USERAGENT,userAgentMozilla);        // set the user agent
    curl_easy_setopt(handle,CURLOPT_URL,"http://myWebsite.com/logIn");     // set the url
    curl_easy_perform(handle);                              // perform
                                                            // I must to take cookies that the website gave to me.
    curl_easy_setopt(handle,CURLOPT_POSTFIELDSIZE,58L);     // size of the request.
    curl_easy_setopt(handle,CURLOPT_POSTFIELDS,data);       // aray of the POST request
    curl_easy_perform(handle);                              // perform and connect to the Website

}

我也使用Wireshark,

当我手动完成时(我只能打印)

$QU9Eq@*Y!P9?PDpPOST /site/doLogIn HTTP/1.1

我不知道它是什么。

Host: myWebsite.com

主持人。

Connection: keep-alive
Content-Length: 140
Accept: text/html, */*; q=0.01

我认为有些手续。

Origin: http://myWebsite.com

再次主持。与类型。

X-Requested-With: XMLHttpRequest

我不知道它是什么。

User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36

用户代理。我真的不明白为什么有4种类型的用户代理。

Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://myWebsite/logIn
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4

手续。

Cookie: advertisementcookie=1; __utma=108225430.1356423961.1414526372.1414789248.1414921274.6; __utmz=108225430.1414526372.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); sessionprod=oekotlo6tfbujtpfirddvvqlp1; _gat=1; _ga=GA1.2.1356423961.1414526372

嗯,那开始是强生的。这是在HTTP请求期间发送到网站的cookie。每次发送新的HTTP请求时,它们都会更改。

&to=gaia&login=myName&password=myPassword&redirection=http%3A%2F%2FmyWebsite.com%2%2F%3Fidentification%3D1&isBoxStyle=&08e11696a1=

这是我的要求的主要内容。名称和密码将发送到服务器。

我没有错误,但我的代码无法正常工作,因为我上次连接的时间没有变化...可能是因为我没有设置cookie。 我真的没经验,我知道我可能做的很多愚蠢的错误。但是,如果您有任何信息,请回答我的问题...... 你有一些修复这个代码的好主意吗?

祝你好运。 济。

2 个答案:

答案 0 :(得分:0)

您想要设置字符串&#34;&amp; to = gaia&amp; login = myName&amp; password = myPassword&amp; redirection = http%3A%&#34; ...对于{&#34;常规&#34;的CURLOPT_POSTFIELDS选项POST。

您使用的CURLOPT_HTTPPOST选项(与curl_formadd等结合使用)适用于多部分formposts,您显然不想要它!

答案 1 :(得分:0)

我很高兴告诉你我终于做到了。 感谢Daniel为您的图书馆和您网站上的示例做准备:

char *data = &togaia&login=login&password=pswd&isBoxStyle=
const char *userAgentMozilla = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
CURLcode res;
CURLcode err;

curl_easy_setopt(handle,CURLOPT_COOKIESESSION,1);       // clean up session.
curl_easy_setopt(handle,CURLOPT_COOKIEJAR,"cookie.txt");  // save cookie. use it for each action on a website when you are register, juste doing curl_easy_setopt(handle,CURLOPT_COOKIE,"cookie.txt");
curl_easy_setopt(handle,CURLOPT_USERAGENT,userAgentMozilla);        // set the user agent
curl_easy_setopt(handle,CURLOPT_URL,"http://thewebsite/site/doLogIn");     // set the url                         // perform
curl_easy_setopt(handle,CURLOPT_POSTFIELDSIZE,strlen(data));     // size of the request.
curl_easy_setopt(handle,CURLOPT_POSTFIELDS,data);       // aray of the POST request
res = curl_easy_perform(handle);                              // perform and connect to the Website

如果您有问题,请毫不犹豫地发帖,我会尽力帮助您:) 这是连接到网站的答案,你也可以看一下我嗅到的例子;)