Search code examples
csocketsraspberry-piraspbian

503 - Service temporarily unavailable


I need simple HTTP client on Raspberry PI zero with Raspbian. I used few example codes, but when i send about 7 requests then i can download just page with this error: 503 Service temporarily unavailable There is no available fastcgi process to fullfill your request.

One of used codes:

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#define SA      struct sockaddr
#define MAXLINE 4096
#define MAXSUB  200

ssize_t process_http(int sockfd, char *host, char *page)
{
    ssize_t n;
      snprintf(sendline, MAXSUB,
                 "GET %s\r\n"
                 "Host: %s\r\n"
                 "Connection: close\n"
                "\n", page, host);

        write(sockfd, sendline, strlen(sendline));

        while ((n = read(sockfd, recvline, MAXLINE)) > 0)
         {
    recvline[n] = '\0';
    }
        printf("%s", recvline);


        return n;
}

and in main is this:

 int sockfd;
        struct sockaddr_in servaddr;

        char **pptr;

        char *hname = "plankter.cz";
        char *page = "http://plankter.cz/iot/list.json";

        char str[50];
        struct hostent *hptr;
        if ((hptr = gethostbyname(hname)) == NULL) {
                fprintf(stderr, " gethostbyname error for host: %s: %s",
                        hname, hstrerror(h_errno));
                exit(1);
        }
        printf("hostname: %s\n", hptr->h_name);
        if (hptr->h_addrtype == AF_INET
            && (pptr = hptr->h_addr_list) != NULL) {
                printf("address: %s\n",
                       inet_ntop(hptr->h_addrtype, *pptr, str,
                                 sizeof(str)));
        } else {
                fprintf(stderr, "Error call inet_ntop \n");
        }

        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(80);
        inet_pton(AF_INET, str, &servaddr.sin_addr);

        connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
        process_http(sockfd, hname, page);
        close(sockfd);

In this example i download json, but when i read php or txt, i have same problem. I tried some another example codes using sockets, example with happyhttp library and all give me same result after 7 requests. I think it doesn't close connection. I need to send http request and recieve data, and i need to do it few times in minute.

Thanks for all ideas.


Solution

  • You provide a full URI to the GET field:

    char *page = "http://plankter.cz/iot/list.json";
    ...
    snprintf(sendline, MAXSUB,
                "GET %s\r\n"
                "Host: %s\r\n"
                "Connection: close\n"
                "\n", page, host);
    

    You should not include protocol and host name.

    Try this instead:

    char *page = "/iot/list.json";