所以我有这个脚本将条形码发送到服务器(在收到条形码时应该用代码200响应)。
我想知道的是我如何在我的C脚本中检查这一点。
例如
if(ServerResponse == 200)
printf(您的条形码已被传输)
我的英语不好以及我的C知识非常糟糕
#include <stdio.h >
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
while (1)
{
char buf[256],syscmd[512];
int i;
/* Get next barcode */
printf("Waiting for bar code [q=quit]: ");
if (fgets(buf,255,stdin)==NULL)
break;
/* Clean CR/LF off of string */
for (i=0;buf[i]!='\0' && buf[i]!='\r' && buf[i]!='\n';i++);
buf[i]='\0';
/* q = quit */
if (!strcmp(buf,"q"))
break;
/* Build into curl command */
sprintf(syscmd,"curl \"http://www.xyz.com/test/order/complete?barcode=%s\"",buf);
/* Execute--this will wait for command to complete before continuing. */
system(syscmd);
}
return(0);
}
答案 0 :(得分:0)
我会调查popen,因为curl会默认将数据打印到stdout(如果不是,请查看man curl),popen会让你读取数据。
HTTP标头(http://www.w3.org/Protocols/rfc2616/rfc2616.html)描述了该代码的确切位置。另外,我建议确保数据不会溢出syscmd。
char *base = "curl \"http://www.xyz.com/test/order/complete?barcode=\"";
if (strlen(base) + strlen(buf) > 511)
{
fprintf(stderr, "Error! buffer overflow would have happened!\n");
return 1;
}
else
{
sprintf(syscmd, "curl \"http://www.xyz.com/test/order/complete?barcode=%s\"", buf);
FILE *p = popen(syscmd, "r");
//check the output for HTTP code 200 here
}
希望对你有所帮助。
编辑:检查代码也非常简单
//assume you are using the code from above
int code;
fscanf(p, "HTTP/1.1 %d", &code); //either HTTP/1.1 or HTTP/1.0 depending on your server
if (code == 200)
{
printf("Yupp, all good :)\n");
}
else
{
printf("Server did not do so well with that request :(\n");
}