//http interface
int http_post_openapi(const char *pIP, const char *pServ, int port, const char *pSendValue, char *pRecvValue){
int sockfd, ret, i, h;
struct sockaddr_in servaddr;
char szHttpHead[1024], buf[8192], szSendValueLength[128], szSendBuffer[4096];
int iLen = 0;
fd_set t_set1;
struct timeval tv;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
printf("Failed to create network connection, this thread will terminate---socket error!\n");
exit(0);
};
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
if (inet_pton(AF_INET, pIP, &servaddr.sin_addr) <= 0 ) {
printf("Failed to create network connection, this thread will terminate--inet_pton error!\n");
exit(0);
};
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
printf("Failed to connect to server, connect error!\n");
exit(0);
}
printf("Connected to the remote host\n");
if (pSendValue == NULL || pRecvValue == NULL) {
close(sockfd);
printf("\nEither pSendValue or pRecvValue is NULL!!!\n");
return -1;
}
char szSendConver[4096] = {0};
TrimAll(pSendValue, szSendConver);
iLen = strlen(szSendConver);
memset(szSendValueLength, 0, 128);
sprintf(szSendValueLength, "%d", iLen);
printf("szSendValueLength after sprintf is :%s\n", szSendValueLength);
memset(szHttpHead, 0, 256);
strcat(szHttpHead, "POST ");
strcat(szHttpHead, pServ);
strcat(szHttpHead, " HTTP/1.1\r\n");
strcat(szHttpHead, "Host: ");
strcat(szHttpHead, pIP);
strcat(szHttpHead, ":");
char cPort[6];
sprintf(cPort,"%ld",port);
strcat(szHttpHead, cPort);
strcat(szHttpHead, "\r\n");
strcat(szHttpHead, "User-Agent: Apache-HttpClient/4.1.1\r\n");
strcat(szHttpHead, "Accept: */*\r\n");
strcat(szHttpHead, "Content-Length: ");
strcat(szHttpHead, szSendValueLength);
strcat(szHttpHead, "\r\n");
strcat(szHttpHead, "Content-Type: application/json; charset=UTF-8");
printf("szSendValueLength is :%s\n", szSendValueLength);
strcat(szHttpHead, "\r\n\r\n");
memset(szSendBuffer, 0, 4096);
strcat(szSendBuffer, szHttpHead);
strcat(szSendBuffer, szSendConver);
strcat(szSendBuffer, "\r\n\r\n");
printf("Print SendBuffer before write :\n%s\n",szSendBuffer);
ret = write(sockfd,szSendBuffer,strlen(szSendBuffer));
if (ret < 0) {
printf("Send failed! Error code is %d, error message is '%s'\n",errno, strerror(errno));
exit(0);
}else{
printf("Message sent successfully, a total of %d bytes were sent!\n\n", ret);
}
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
memset(buf, 0, sizeof(buf));
i= read(sockfd, buf, sizeof(buf)-1);
if (i==0) {
close(sockfd);
printf("Detected that the remote closed the connection while reading the data packet, this thread will terminate!\n");
return -1;
}
close(sockfd);
// Here, find the HTTP RESPONSE result code. If it is 200, then it is successful, extract the body and assign it to pRecvBuff; otherwise, return -1.
char *pRet = NULL;
char *pStart = NULL;
char *pEnd = NULL;
//pRet = strstr(buf, "HTTP/1.1 200 OK");
pRet = strstr(buf, "HTTP/1.1 200");
if(!pRet) {
cout << "HTTP Response Error!!!" << endl;
return -1;
}
string sRecv;
utf82gb(buf, sRecv);
printf("sRecv is :\n%s\n", sRecv.c_str());
memset(buf, 0, sizeof(buf));
strncpy(buf, sRecv.c_str(), sRecv.length());
pStart = strstr(buf, "{");
printf("pStart address is :0x%x\n", pStart);
pEnd = strrchr(buf, '}') + 1;
printf("pEnd address is :0x%x\n", pStart);
// if(pStart != NULL && pEnd != NULL) {
strncpy(pRecvValue, pStart, (int)(pEnd - pStart));
//userlog("Message returned successfully - message, request message:\n%s\n ******** Return message:\n%s\n", szSendBuffer, buf);
//} else {
printf("\nbuf中未找到匹配的数据!!!\n");
userlog("Message return failed - message, request message:\n%s\n ******** Return message:\n%s\n", szSendBuffer, buf);
return -2;
//}
return 0;
}