2014-12-25 1 views
0

내 코드에 어려움이 있습니다. 나는 cURL을 사용하여 웹 사이트에 연결하려고합니다. (즉, 인쇄의 내가 수동으로 할 때 내가 무엇을 가지고,C 코드 : cURL POST

#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 

} 

나는 또한 와이어 샤크를 사용

을 :의 내 코드를 보자, 지금

<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;);"> 

: 의 웹 사이트의 코드를 보자 caract 전용)

[email protected]*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 

글쎄, 그것은 interessant 시작합니다. 이것은 HTTP 요청 중에 웹 사이트에 전송 된 쿠키입니다. 그들은 새로운 HTTP 요청을 보낼 때마다 변경되었습니다.

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

내 요청의 교장입니다. 이름과 암호가 서버로 전송됩니다.

마지막 연결 시간이 변경되지 않았기 때문에 오류가 없지만 내 코드가 작동하지 않습니다 ... 아마 쿠키를 설정하지 않았기 때문일 수 있습니다. 나는 정말로 경험이 없으며 나는 아마도 많은 어리석은 실수를하고 있다는 것을 알고있다. 그러나, 제발, 만약 당신이 어떤 정보를 가지고 내 질문에 대답 ... 이 코드를 수리하기위한 몇 가지 유창한 아이디어가 있습니까?

감사합니다. Xavier.

답변

0

당신은 이러한 "일반"POST에 대한 CURLOPT_POSTFIELDS 옵션 문자열 ... "= 가이아 & 로그인 = MyName로 & 비밀 번호 = myPassword & 리디렉션 = HTTP %의 3A의 %에 &"로 설정합니다.

(curl_formadd 등과 조합하여 사용하는) CURLOPT_HTTPPOST 옵션은 분명히 원하지 않는 멀티 파트 양식 포스트 용입니다!

+0

감사합니다 다니엘. 나는 몇 시간이나 몇 시간 동안 노력없이 성공했다. 그러나 나는 여전히 연결되어 있지 않다. CURLOPT_POSTFIELDS를 사용하여 수동으로 연결할 때와 동일한 패킷을 보내야합니다. 나는 Wireshark와 함께 그 사람을 만날 수 있지만, 나는 왜 그랬는지 모르겠다. 나는 아직 연결되어 있지 않다 ... – AnonBird

+0

글쎄, 그건 네 질문에 따라 요청하는 방법이다. 사이트에서 귀하의 게시물을 수락하도록하려면 여러 측면에서 브라우저를 모방해야 할 수도 있습니다. 쿠키를 사용하는 것이 중요한 부분입니다. 아직도 당신이 요구 한 것이 아닙니다 ... –

+0

대니얼 감사합니다. 그러나 나는 그 모든 일에 어려움을 겪고 있습니다. 나는 당신의 librairies로 시작하고있다 (정말로 강력 할 것 같다!!). 나는 메인 포스트를 편집했다. 나는 브라우저를 모방하는 올바른 선택을했지만 쿠키에 대한 아이디어는 갖고 있다고 생각한다. – AnonBird

0

드디어 해냈습니다. 감사 다니엘 웹 사이트 라이브러리 및 예를 들어 :

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 

당신이 probleme이있는 경우, 아니 게시하는 것을 망설이지 않으며, 난 당신을 도우려고합니다 :) 를이 웹 사이트에 연결하는 대답은, 당신은 또한 예를 들어 내가 킁킁 봐 수 있습니다;)