如何从vc ++项目中查询http请求

如何解决如何从vc ++项目中查询http请求

| 我正在编写一个示例程序来从某个站点获取文件。 但它是在回复我文件未找到错误,我不知道如何制作http标头。 任何人都可以帮助我编写http标头。 而且我试图以这种方式打印ip(printf(\“%s \\ n \”,ptr-> ai_addr-> sa_data);)  但没有得到如何打印IP地址?,我做对了吗? 我的代码是:-
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib,Mswsock.lib,and Advapi32.lib
#pragma comment (lib,\"Ws2_32.lib\")
#pragma comment (lib,\"Mswsock.lib\")
#pragma comment (lib,\"AdvApi32.lib\")


#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT \"80\"

int __cdecl main(int argc,char **argv) 
{
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,*ptr = NULL,hints;
    char *sendbuf = \"GET / HTTP/1.1[CRLF] Host: www.espncricinfo.com[CRLF]Connection: close[CRLF]User-Agent: Web-sniffer/1.0.37 (+http://web-sniffer.net/)[CRLF]Accept-Encoding: gzip[CRLF]Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]Cache-Control: no-cache[CRLF]Accept-Language: de,en;q=0.7,en-us;q=0.3 \\r\\n\";
    char buff[DEFAULT_BUFLEN],recvbuf[512]; 
    int iResult;
    int recvbuflen = DEFAULT_BUFLEN;

    // Validate the parameters
    if (argc != 1) {
        printf(\"usage: %s server-name\\n\",argv[0]);
        return 1;
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
    if (iResult != 0) {
        printf(\"WSAStartup failed with error: %d\\n\",iResult);
        return 1;
    }

    ZeroMemory( &hints,sizeof(hints) );
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    // Resolve the server address and port
    iResult = getaddrinfo(\"122.169.255.9\",DEFAULT_PORT,&hints,&result);
    if ( iResult != 0 ) {
        printf(\"getaddrinfo failed with error: %d\\n\",iResult);
        WSACleanup();
        return 1;
    }

    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

        // Create a SOCKET for connecting to server

        ConnectSocket = socket(ptr->ai_family,ptr->ai_socktype,ptr->ai_protocol);
        printf(\"inside loop %s \",ptr->ai_addr->sa_data);
        if (ConnectSocket == INVALID_SOCKET) {
            printf(\"socket failed with error: %ld\\n\",WSAGetLastError());
            WSACleanup();
            return 1;
        }

        // Connect to server.
        iResult = connect(ConnectSocket,ptr->ai_addr,(int)ptr->ai_addrlen);
        printf(\"%d\\n\",ptr->ai_addrlen);
//      printf(\"%s\\n\",ptr->ai_addr->sa_family);
        printf(\"%s\\n\",ptr->ai_addr->sa_data);

        if (iResult == SOCKET_ERROR) {
            printf(\"socket error\\n\");
            printf(\"failed with error: %d\\n\",WSAGetLastError());
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf(\"Unable to connect to server!\\n\");
        WSACleanup();
        return 1;
    }
    // Send an initial buffer
    char filepath[]=\"pakistan/content/current/story/517271.html\";
    sprintf(buff,\"GET %s\\r\\n\",filepath);
    iResult = send( ConnectSocket,buff,(int)strlen(buff),0 );
    if (iResult == SOCKET_ERROR)
    {
        printf(\"send failed with error: %d\\n\",WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    printf(\"Bytes Sent: %ld\\n\",iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket,SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf(\"shutdown failed with error: %d\\n\",WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    } 

    do 
    {
        iResult = recv(ConnectSocket,recvbuf,recvbuflen,0);
        //if ( iResult > 0 ) printf(\"Bytes received: %d\\n\",iResult);
         if ( iResult > 0 )
         {
            printf(\"Bytes received: %d\\n\",iResult);
            printf(\"%s\",recvbuf);
         }
         else if ( iResult == 0 )
            printf(\"Connection closed\\n\");
        else
            printf(\"recv failed with error: %d\\n\",WSAGetLastError()); 

    } while( iResult>0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}
    

解决方法

        如果您要使用Microsoft Windows函数,则WinInet中的HTTP函数要容易得多。请参阅HttpOpenRequest / HttpSendRequest / HttpQueryInfo / InternetReadFile     ,        如果您正在获取“找不到文件”,那是因为您正在请求找不到文件。 也许尝试以斜杠开头的
\"/pakistan/content/current/story/517271.html\"
。 您的
HTTP
协议说明符在哪里? 写:
GET path HTTP/1.0
(1.0是因为您可能不想弄乱虚拟主机;如果需要,则是1.1) 为何在尝试从套接字读取之前关闭套接字...?我知道您只关闭了\“ send \”管道,但这仍然很奇怪。 也许您应该使用预先建立的库来为您执行此操作。 位于http://122.169.255.9/的Web服务器似乎无法正常工作。也许首先使用http://www.google.com之类的知名服务器进行测试。     ,        如果您不想重新发明轮子,请使用libcurl之类的库。     ,        除了Tomalak Geret'kal 您将要使用HTTP / 1.0,除非您要检查分块的内容,否则HTTP 1.1会给您带来更多麻烦。 安装Wireshark并记录请求。您会发现哪里出了问题。 我不知道[CRLF]被自动更改为\\ r \\ n     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-