2017-10-26 6 views
1

次のコードスニペットが見つかり、jsonデータをre_urlにポストするための変更はほとんどありませんでした。しかし、エラー接続のエラーを示しています...サーバは8888ポートでjsonデータを待ち受けます。jsonデータからhttps in c

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

#define APIKEY "YOUR_API_KEY" 
#define HOST "https://YOUR_WEB_SERVER_URI" 
#define PORT "8888" 
char *s = "somejson"; 
int main() { 

// 
// Initialize the variables 
// 
BIO* bio; 
SSL* ssl; 
SSL_CTX* ctx; 

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

// 
// Creates a new SSL_CTX object as a 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); 

// 
// Use the variable from the beginning of the file to create a 
// string that contains the URL to the site that you want to connect 
// to while also specifying the port. 
// 
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"); 
} 

// 
// The bare minimum to make 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 writes here 
    // 
    if(!BIO_should_retry(bio)) 
    { 
     // Not worth implementing, but worth knowing. 
    } 

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

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

// 
// Read the response message 
// 
for(;;) 
{ 
    // 
    // Get chunks of the response 1023 at the time. 
    // 
    size = BIO_read(bio, buf, 1023); 

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

    // 
    // Terminate the string with a 0, to let know C when the string 
    // ends. 
    // 
    buf[size] = 0; 

    // 
    // -> Print out the response 
    // 
    printf("%s", buf); 
} 

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

return 0; 
} 

上記のコードでは、リモートサーバーとのTLS接続を確立する方法について詳細に説明します。

重要:このコードでは、公開鍵が有効な機関によって署名されているかどうかはチェックされません。意味私は検証のためにルート証明書を使用しません。このチェックを実装することを忘れないでください。適切なウェブサイトに接続しているかどうかわかりません。

リクエスト自体について言えば、 HTTPリクエストを手作業で書くことはこれ以上ありません。

このリンクの下で、システムにopenSSLをインストールする方法と、セキュアライブラリを使用するようにコードをコンパイルする方法について説明しています。 BIO_set_conn_hostnameから

+0

あなたのコードの下にあるこのテキストは、すべて意味するものですか?あなたはあなたの質問に無関係などこかからそれを単にコピーしましたか?最後の文でどのリンクが参照されていますか? – Gerhardh

+0

はい別の質問からそのままコピーしました..今解決しました.. –

答えて

0

BIO_set_conn_hostname()は、ホスト名を設定するには、文字列名を使用しています。 のホスト名にはIPアドレスを使用できます。ホスト名には、ホスト名:ポートの形式でポート を含めることもできます。 "hostname/any/other/path"または "hostname:port/any/other/path"という形式を使用することもできます。

#define HOST "https://YOUR_WEB_SERVER_URI" 
... 
// Use the variable from the beginning of the file to create a 
// string that contains the URL to the site that you want to connect 
// to while also specifying the port. 
BIO_set_conn_hostname(bio, HOST ":" PORT); 

コメントが正しくURLを参照する代わりに、あなたはおそらく間違っているURIを提供します。

あなたは有効なホスト名を提供しません。

YOUR_WEB...を実際のホスト名に置き換えた場合、それは依然としてURIであり、ホスト名ではありません。 "https://"部分を削除し、ホスト名のみを入力してください。

+0

接続を確立することができましたが、jsonが正しい形式であるにもかかわらずレスポンスが悪い要求になりました。サーバがbjsonに期待しています –

+0

あなたはjsonのものをまったく送っていないことに気付いていますか?接続を確保せずにjsonとサーバーの内容を確認しましたか?あなたは「悪い要求」をどこで得るのですか?関数呼び出しからの戻り値? HTTPの結果ですか?あなたの説明をもっと正確にしてください。 – Gerhardh

+0

はい..ここでは間違いですが、私は上の答えでこれを解決することができました..ありがとうございました... –

関連する問題