前言
JSON数据格式是http协议POST传输方式中的一种。
目录
- 常见的HTTP/1.1请求数据
- POST JSON数据
C代码
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <math.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include "cJSON/cJSON.h"
/*
POST /.../... HTTP/1.1
Host: ...:...
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
Content-Length: 78
{
...
}
*/
// POST JSON数据
HttpResponse *postJson(const char *ip, int port, const char *subUrl, const char *jsonStr) {
//LOGD("postJson(): IP= %s , PORT= %d , SUB_URL= %s , JSON= %s", ip, port, subUrl, jsonStr);
HttpResponse *_httpResponse = (HttpResponse *) malloc(sizeof(HttpResponse));
memset(_httpResponse, 0, sizeof(HttpResponse));
_httpResponse->code = strdup("500");
int _sockfd;
int _len;
struct sockaddr_in _address;
int _ret;
char *_sendStr = (char *) malloc(1024 * 1024);
memset(_sendStr, 0, 1024 * 1024);
char _str1[1024] = {0};
char *_recvStr = (char *) malloc(1024 * 1024);
memset(_recvStr, 0, 1024 * 1024);
_sockfd = socket(AF_INET, SOCK_STREAM, 0);
_address.sin_family = AF_INET;
_address.sin_addr.s_addr = inet_addr(ip);
_address.sin_port = htons(port);
// 超时时间设置
struct timeval _tm;
_tm.tv_sec = 0;
_tm.tv_usec = 500000;
// 设置发送超时
setsockopt(_sockfd, SOL_SOCKET, SO_SNDTIMEO, &_tm, sizeof(struct timeval));
// 设置接收超时
setsockopt(_sockfd, SOL_SOCKET, SO_RCVTIMEO, &_tm, sizeof(struct timeval));
_len = sizeof(_address);
_ret = connect(_sockfd, (struct sockaddr *) &_address, _len);
if (_ret < 0) {
//LOGE("postJson(): connect ERROR, ERROR= %s, IP= %s , PORT= %d", strerror(errno), ip, port);
free(_sendStr);
_sendStr = NULL;
free(_recvStr);
_recvStr = NULL;
return _httpResponse;
}
strcpy(_sendStr, "");
strcat(_sendStr, "POST ");
strcat(_sendStr, subUrl);
strcat(_sendStr, " HTTP/1.1\r\n");
strcat(_sendStr, "Host: ");
strcat(_sendStr, ip);
strcat(_sendStr, ":");
sprintf(_str1, "%d", port);
strcat(_sendStr, _str1);
strcat(_sendStr, "\r\n");
strcat(_sendStr,"Connection: keep-alive\r\n");
strcat(_sendStr, "Content-Type: application/json; charset=UTF-8\r\n");
strcat(_sendStr, "Content-Length: ");
_len = strlen(jsonStr);
sprintf(_str1, "%d", _len);
strcat(_sendStr, _str1);
strcat(_sendStr, "\r\n\r\n");
// jsonStr的值为post的数据
strcat(_sendStr, jsonStr);
_len = write(_sockfd, _sendStr, strlen(_sendStr));
//LOGD("postJson(): write LENGTH= %d", _len);
int _readSize = 0;
while (1) {
_len = read(_sockfd, _str1, 1024);
//LOGD("postJson(): read LENGTH= %d", _len);
if (_len <= 0) {
break;
}
memcpy(_recvStr + _readSize, _str1, _len);
_readSize += _len;
// 超时时间设置,后续包耗时较小
_tm.tv_sec = 0;
_tm.tv_usec = 10000;
// 设置接收超时
setsockopt(_sockfd, SOL_SOCKET, SO_RCVTIMEO, &_tm, sizeof(struct timeval));
}
free(_sendStr);
_sendStr = NULL;
close(_sockfd);
if (strlen(_recvStr) == 0) {
} else {
releaseHttpResponse(_httpResponse);
_httpResponse = parseResponse(_recvStr);
}
free(_recvStr);
_recvStr = NULL;
return _httpResponse;
}
觉得有用的话,可以给我点奖励哦!(微信)