2012-02-22 3 views
2

C를 사용하여 HTTP 연결을 만드는 방법은 무엇입니까? 모든 참조 또는 샘플 코드가 있습니까? 또한 C로 작성된 클라이언트에서 Comet 연결 열기를 구현하려면 어떻게해야합니까? (HTTPS 연결을 여는 것에 대한 추가 정보도 감사하겠습니다.) 감사합니다!C를 사용하여 HTTP 연결을 만드는 방법은 무엇입니까?

+3

당신은 [libcurl에]를 찾고 있습니다 (http://curl.haxx.se/). – Jon

답변

2

으로 이것을 시도, 좋은 참조 라이브러리를 필요 자신의 http 클라이언트, 그렇지 않으면 컬 같은 컬트 라이브러리를 선택할 수 있습니다. 당신은 인코딩과 각 요청과 함께 헤더 정보를 디코딩 할 필요가 다음 일반 소켓을 사용하는 경우 은 또한 http 프로토콜에 대해 너무 많은 다른 요인 http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

1

이 오래된 주제를 고려할 필요하지만 확실히 하나 즉 봤 거든. 몇 주간의 연구 끝에이 주제에 대해 읽고 복잡한 예제와 튜토리얼을 살펴보십시오. 나는 그것을 작동시키기 위해 필요한 최소한으로 삶아 냈다.

중요 사항 :이 코드는 공개 키가 유효한 기관에 의해 서명되었는지 여부를 확인하지 않습니다. 의미 나는 유효성 검사를 위해 루트 인증서를 사용하지 않습니다.

아래 코드는 빌드 한 더 큰 프로젝트의 일부입니다. README.md에서 openSSL을 설치하는 방법과 코드를 컴파일하는 방법에 대한 추가 정보를 확인할 수 있습니다.

#include <stdio.h> 
#include <openssl/ssl.h> 
#include <openssl/err.h> 
#include <openssl/bio.h> 

#define APIKEY "YOUR_API_KEY" 
#define HOST "YOUR_WEB_SERVER_URI" 
#define PORT "443" 

int main() { 

    // 
    // Initialize Variables 
    // 
    BIO* bio; 
    SSL* ssl; 
    SSL_CTX* ctx; 

    // 
    // Registers the available SSL/TLS ciphers and digests. 
    // 
    // Basically start the security layer. 
    // 
    SSL_library_init(); 

    // 
    // Creates a new SSL_CTX object as framework to establish TLS/SSL 
    // or DTLS enabled connections 
    // 
    ctx = SSL_CTX_new(SSLv23_client_method()); 

    // 
    // -> Error check 
    // 
    if (ctx == NULL) 
    { 
     printf("Ctx is null\n"); 
    } 

    // 
    // Creates a new BIO chain consisting of an SSL BIO 
    // 
    bio = BIO_new_ssl_connect(ctx); 

    // 
    // uses the string name to set the hostname 
    // 
    BIO_set_conn_hostname(bio, HOST ":" PORT); 

    // 
    // Attempts to connect the supplied BIO 
    // 
    if(BIO_do_connect(bio) <= 0) 
    { 
     printf("Failed connection\n"); 
     return 1; 
    } 
    else 
    { 
     printf("Connected\n"); 
    } 

    // 
    // Data to send to create a HTTP request. 
    // 
    char* write_buf = "POST/HTTP/1.1\r\n" 
         "Host: " HOST "\r\n" 
         "Authorization: Basic " APIKEY "\r\n" 
         "Connection: close\r\n" 
         "\r\n"; 

    // 
    // Attempts to write len bytes from buf to BIO 
    // 
    if(BIO_write(bio, write_buf, strlen(write_buf)) <= 0) 
    { 
     // 
     // Handle failed write here 
     // 
     if(!BIO_should_retry(bio)) 
     { 
      // Not worth implementing, but worth knowing. 
     } 

     // 
     // -> Let us know about the failed write 
     // 
     printf("Failed write\n"); 
    } 

    // 
    // Variables used to read the response from the server 
    // 
    int size; 
    char buf[1024]; 

    // 
    // Read the response message 
    // 
    for(;;) 
    { 
     // 
     // Put response in a buffer of size. 
     // 
     size = BIO_read(bio, buf, 1023); 

     // 
     // If no more data, then exit the loop 
     // 
     if(size <= 0) 
     { 
      break; 
     } 

     // 
     // Terminate the string with a 0 so the system knows where 
     // the end is. 
     // 
     buf[size] = 0; 

     printf("%s", buf); 
    } 

    // 
    // Clean after ourselves 
    // 
    BIO_free_all(bio); 
    SSL_CTX_free(ctx); 

    return 0; 
} 
관련 문제