Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Edit User-Agent in C http request

I encounter bad request when I try to do a GET request with a C code on my apache2 webserver. Here is the log :

92.184.98.135 - - [20/Jul/2021:11:52:17 +0000] "GET //log.php?log=test&name=name HTTP/1.1" 400 0 "-" "-"

And when i do the same with Firefox it works :

92.184.98.135 - - [20/Jul/2021:11:59:17 +0000] "GET //log.php?log=log&name=name HTTP/1.1" 200 203 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"

Anyone knows how to disable User-Agent check with apache2 server ? Or if the problem comes from somewhere else ?

Here is the code of my function in c :

WSADATA wsa;
SOCKET sockfd;
char *message_fmt = "GET //log.php?log=%s&name=%s HTTP/1.1\r\n\r\n";
struct hostent *server;
struct sockaddr_in serv_addr;
int bytes, sent, received, total;
char message[4096],response[4096];

FILE * LOG_FILE;
LOG_FILE = fopen(file, "r+");

char log[1000] = "data:";
char ch = fgetc(LOG_FILE);

while(ch != EOF) {
    strncat(log, &ch, 1); 
    ch = fgetc(LOG_FILE);
}

fclose(LOG_FILE);

char *username = getenv("USERNAME");

sprintf(message,message_fmt,log,username);

if((sockfd = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        fprintf(DEBUG,"Could not create socket : %d" , WSAGetLastError());
    }

server = gethostbyname(adress);

memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);

connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr);
total = strlen(message);
sent = 0;
do {
    bytes = send(sockfd,message+sent,total-sent,0);
    if (bytes < 0)
        {printf("ERROR writing message to socket");}
    if (bytes == 0)
        break;
    sent+=bytes;
} while (sent < total);

memset(response,0,sizeof(response));
total = sizeof(response)-1;
received = 0;
do {
    bytes = recv(sockfd,response+received,total-received,0);
    if (bytes < 0)
        {printf("ERROR reading response from socket");}
    if (bytes == 0)
        break;
    received+=bytes;
} while (received < total);

closesocket(sockfd);
WSACleanup();

As far as this code is for Windows, I use these librairies :

#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib, "Ws2_32.lib")
#include <windows.h>
#include <winuser.h>
#include <string.h>

Thank you for your help !

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda