Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

CでIFTTTのMaker にProxy経由でアクセスする

More than 3 years have passed since last update.




http://qiita.com/edo_m18/items/cef278d0c14d1371db3b を参考にしています。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <unistd.h>

#define proxy_url "*******" //Proxyサーバの名前を入力
#define proxy_port 8080

#define BUF_LEN 256

struct URL {
    char host[BUF_LEN];
    char path[BUF_LEN];
    char query[BUF_LEN];
    char fragment[BUF_LEN];
    unsigned short port;

int main(int argc, char **argv) {

    // ソケットのためのファイルディスクリプタ
    int s;

    // IPアドレスの解決
    struct addrinfo hints, *res;
    struct sockaddr_in *addr_in;
    struct in_addr addr;
    int err;

    // サーバに送るHTTPプロトコル用バッファ
    char send_buf[BUF_LEN];
    struct URL url = {
    }; //内容は適宜変更

    printf("http://%s%s%s を取得します。\n\n", url.host, url.path, url.query);

     // 0クリア
     memset(&hints, 0, sizeof(hints));
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_family   = AF_INET;

     char *serviceType = "http";

#ifdef proxy_url
    if ((err = getaddrinfo(proxy_url, serviceType, &hints, &res)) != 0){
        printf("error %d\n", err);
        return 1;
    if ((err = getaddrinfo(url.host, serviceType, &hints, &res)) != 0) {
        printf("error %d\n", err);
        return 1;

    // ソケット生成
    if ((s = socket(res->ai_family, res->ai_socktype,res->ai_protocol)) <0) {
         fprintf(stderr, "ソケットの生成に失敗しました。\n");
         return 1;

#ifdef proxy_port
     addr_in = (struct sockaddr_in *)(res -> ai_addr);
     addr_in -> sin_port=htons(proxy_port); #endif

     // サーバに接続
     if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
         fprintf(stderr, "connectに失敗しました。\n");
         return 1;

     // HTTPプロトコルの開始 &サーバに送信
#ifdef proxy_url
    sprintf(send_buf, "POST http://%s%s%s HTTP/1.0\r\n", url.host, url.path, url.query); #else
    sprintf(send_buf, "POST %s%s HTTP/1.0\r\n", url.path, url.query);
    write(s, send_buf, strlen(send_buf));
    sprintf(send_buf, "Host: %s:%d\r\n", url.host, url.port);
    write(s, send_buf, strlen(send_buf));

    sprintf(send_buf, "\r\n");
    write(s, send_buf, strlen(send_buf));
    // 受信が終わるまでループ
    while(1) {
        char buf[BUF_LEN];
        int read_size;
        read_size = read(s, buf, BUF_LEN);

        if (read_size > 0) {
            write(1, buf, read_size);
        else {

    // ソケットを閉じる
    return 0;

Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Hello Worldの次の世界へ mbedはこっち: https://developer.mbed.org/users/yueee_yt/


No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
Help us understand the problem. What is going on with this article?