我尝试使用getopt从命令行输入一个url,用于HTTP get,post,put和delete。 HTTP GET之前工作正常,但出于某种原因,我无法弄清楚如何解析网址,以便HTTP GET现在成功。当我打印网址时,它表明它处于预期的格式,但卷曲抱怨说主机名无法解析。有人可以向我解释如何解决这个问题,以及如何使它只有在命令行中输入的第一个URL被接受?谢谢!
#include<stdio.h>
#include<getopt.h>
#include<curl/curl.h>
#include<stdlib.h>
#define OK 0
#define INIT_ERR 1
#define REQ_ERR 2
// function for HTTP GET
void get(char* url, CURL *curl, CURLcode res) {
long http_code = 0;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "[CURL] Could not execute HTTP GET: %s\n", curl_easy_strerror(res));
//return REQ_ERR;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
printf("[HTTP CODE]: %ld\n", http_code);
curl_easy_cleanup(curl);
}
}
// function for HTTP PUT
void put() {
}
// function for HTTP POST
void post() {
}
// function for HTTP delete
void delete() {
}
int main(int argc, char **argv) {
int c;
char *content, *url;
CURL *curl;
CURLcode res;
while(1) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"get", no_argument, 0, 'g'},
{"url ", required_argument, 0 , 'u'},
{"post", required_argument, 0, 'o'},
{"put", required_argument, 0, 'p'},
{"delete", required_argument, 0, 'd'},
{0,0,0,0}
};
// getopt stores option index here
int long_index = 0;
c = getopt_long_only(argc, argv, "hgo:p:d:u:", \
long_options, &long_index);
// detect end of options
if(c == -1)
break;
switch(c) {
case 'h':
printf("\n[HELP] Usage: ./test [VERB] 'content' [URL] <url>\n\n");
printf("----------------------- HELP OPTIONS -----------------------------------------\n\n");
printf(" FLAGS ARGUMENTS EXAMPLE ARG FORMAT SUMMARY\n");
printf("------------------------------------------------------------------------------\n");
printf("-h/--help N/A N/A help options\n");
printf("-g/--get N/A N/A [VERB] http get option\n");
printf("-o/--post 'content' 'Post this stuff.' [VERB] http post option\n");
printf("-p/--put 'content' 'Put this stuff.' [VERB] http put option\n");
printf("-d/--delete 'content' 'Delete this stuff/' [VERB] http delete option\n");
printf("-u/--url 'url' 'http://www.cnn.com' [URL] http url of server\n");
printf(" 'http://localhost:PORT' \n");
printf(" 'http://IPADDRESS:PORT'\n\n");
printf("------------------------------------------------------------------------------\n");
printf(" Example USAGE\n");
printf("------------------------------------------------------------------------------\n");
printf("HTTP GET - ./test --get --url 'http://www.cnn.com\n");
printf("HTTP POST - ./test --post 'Post this content.' --url 'http://localhost:8080\n");
printf("HTTP PUT - ./test --put 'Put this content.' --url 'http://localhost:8080\n");
printf("HTTP DELETE - ./test --delete 'Delete this content.' --url 'http://localhost:8080'\n\n");
break;
case 'g':
printf("[HTTP GET]\n");
url;
get(url, curl, res);
break;
case 'u':
url = optarg;
printf("[HTTP URL] url: %s\n", url);
break;
case 'o':
content = optarg;
printf("[HTTP POST] content: %s\n", content);
break;
case 'p':
content = optarg;
printf("[HTTP PUT] content: %s\n", content);
break;
case 'd':
content = optarg;
printf("[HTTP DELETE] content: %s\n", content);
break;
case '?':
//getopt_long already printed error message
break;
default:
abort();
}
//get(url, curl, res);
}
return OK;
}
答案 0 :(得分:0)
正如@MichaelBurr所指出的那样,-g应该根据这段代码在--url / -u之后给出。我试过./test --url“hxxp://google.com”-g并且正在运行。 您可能需要检查localhost:8000