本文為您提供了C語言版本的提交查詢接口對(duì)接DEMO示例
//linux下的編譯:gcc -o test ./test.c
//linux下的執(zhí)行:./test
#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 <netdb.h>
#define host "api.ihuyi.com"
#define PORT 80
#define BUFSIZE 4096
int main(int argc, char **argv)
{
int sockfd, ret, i, h,srandnum;
struct sockaddr_in servaddr;
char str1[4096], str2[4096], buf[BUFSIZE], *str;
socklen_t len;
fd_set t_set1;
struct timeval tv;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { //創(chuàng)建套接字
printf("create connect error!\n"); //創(chuàng)建網(wǎng)絡(luò)連接失敗
exit(0);
};
bzero(&servaddr, sizeof(servaddr)); //每個(gè)字節(jié)都用0填充
servaddr.sin_family = AF_INET; //使用IPv4地址
servaddr.sin_port = htons(PORT); //端口
if (inet_aton(host, &servaddr.sin_addr) == 0){
struct hostent *he;
he = gethostbyname(host);
if (he == NULL)
return -1;
memcpy(&servaddr.sin_addr, he->h_addr, sizeof(struct in_addr));
}
char ipbuf[128];
strncpy(ipbuf, inet_ntoa(servaddr.sin_addr), 128); //將域名轉(zhuǎn)成IP
if (inet_pton(AF_INET, ipbuf, &servaddr.sin_addr) <= 0 ){
printf("inet_pton error!\n"); //創(chuàng)建網(wǎng)絡(luò)連接失敗
exit(0);
};
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
printf("connect error!\n");
exit(0);
}
printf("connect success!\n\n");
//發(fā)送數(shù)據(jù)
memset(str2, 0, 4096);
strcat(str2, "account=xxxxxxxx&password=xxxxxxxxx&name=張三&id_card_no=40122111******&time=1623643787&");
str=(char *)malloc(128);
len = strlen(str2);
sprintf(str, "Content-Length: %d\r\n\r\n", len);
memset(str1, 0, 4096);
strcat(str1, "POST /idcard/id2/Submit.json HTTP/1.1\r\n");
strcat(str1, "Host: ");
strcat(str1, host);
strcat(str1, "\r\n");
strcat(str1, "Content-Type: application/x-www-form-urlencoded\r\n");
strcat(str1, str);
strcat(str1, str2);
strcat(str1, "\r\n\r\n");
printf("Request Data: \n%s\n",str1);
ret = write(sockfd,str1,strlen(str1));
if (ret < 0) {
printf("send data fail!errno:%d, errmsg:'%s'\n\n",errno, strerror(errno));
exit(0);
}else{
printf("send data success, length:%d byte!\n\n", ret);
}
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
while(1){
//sleep(1);
tv.tv_sec= 0;
tv.tv_usec= 0;
h = select(sockfd+1, &t_set1, NULL, NULL, &tv);
if (h == 0) {
memset(buf, 0, 4096);
i= read(sockfd, buf, 4095);
if (i==0){
close(sockfd);//關(guān)閉套接字
printf("0.connect close!\n"); //讀取數(shù)據(jù)報(bào)文時(shí)發(fā)現(xiàn)遠(yuǎn)端關(guān)閉
return -1;
}
printf("Response Data: \n%s\n", buf);
close(sockfd);//關(guān)閉套接字
printf("1.connect close!\n");
return 1;
}
if (h > 0) {
close(sockfd);//關(guān)閉套接字
printf("2.connect close!\n");
return -1;
};
if (h < 0) {
close(sockfd);//關(guān)閉套接字
printf("3.connect close!\n"); //在讀取數(shù)據(jù)報(bào)文時(shí)SELECT檢測(cè)到異常
return -1;
};
//continue;
//break;
}
close(sockfd);//關(guān)閉套接字
printf("4.connect close!\n");
return 0;
}
國(guó)內(nèi)專業(yè)互聯(lián)網(wǎng)團(tuán)隊(duì)
21年行業(yè)經(jīng)驗(yàn)
7x24小時(shí)售后支持
豐富的行業(yè)經(jīng)驗(yàn)
互 億 無 線 官 方 微 信 號(hào)
Copyright ? 2004-2025 上海思銳信息技術(shù)有限公司 All rights reserved. 滬ICP備07035915號(hào)-15 電信增值業(yè)務(wù)許可證:B2-20160082
服務(wù)熱線:
4008 808 898
服務(wù)熱線(工作時(shí)間):
4008 808 898
業(yè)務(wù)咨詢(非工作時(shí)間):
售后咨詢(非工作時(shí)間):
驗(yàn)證碼已發(fā)送到您的手機(jī),請(qǐng)查收!
輸入驗(yàn)證碼后,點(diǎn)擊“開通體驗(yàn)賬戶”按鈕可立即開通體驗(yàn)賬戶。