欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

c 服務端post json數據庫

錢瀠龍1年前8瀏覽0評論

C服務端post json數據庫的方法如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <netdb.h>
#include <ctype.h>
#define SERVER_PORT 80
int main(int argc, char *argv[])
{
int sockfd, ret, len;
struct sockaddr_in servaddr;
char *sendline, *recvline;
char *json;
// 創建socket套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd< 0) {
perror("Socket Create Failed!");
exit(-1);
}
// 清空servaddr結構體
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
// 設置IP地址
if(inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr)<= 0) {
perror("inet_pton error");
exit(-1);
}
// 連接服務器
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))< 0) {
perror("connect error");
exit(-1);
}
// 構建json字符串
json = "{"
"\"name\":\"example\","
"\"age\":20,"
"\"gender\":\"female\","
"\"address\":\"Shanghai\","
"\"phone\":\"12345678900\""
"}";
// 構建http post請求消息
len = strlen(json);
sendline =
"POST /insert HTTP/1.1\r\n"
"Host: localhost\r\n"
"Content-Type: application/json;charset=utf-8\r\n"
"Content-Length: %d\r\n\r\n%s";
// 將json數據放入請求消息中
asprintf(&sendline, sendline, len, json);
// 發送post請求
if(write(sockfd, sendline, strlen(sendline))< 0) {
printf("Send Failed!");
exit(-1);
}
// 釋放資源
free(sendline);
close(sockfd);
return 0;
}