C++实现Host请求_达内
来源:互联网 发布:速达软件问题 编辑:程序博客网 时间:2024/06/05 17:33
各个文件代码:
//> downdata.h
#ifndef DOWNDATA_H_INCLUDED
#define DOWNDATA_H_INCLUDED
#define BUFFSIZE_H 1024
#ifdef __cpluscplus
extern "C"
{
#endif
/**
* @brief 向网络请求资源,返回值存储到当前目录的临时文件whether.txt中。
* @param pUrl - 请求的URL.
* @return if get data return true, else if get nothing return false.
*/
bool WEBQuest(const char * pUrl);
#ifdef __cpluscplus
}
#endif
#endif // DOWNDATA_H_INCLUDED
//> downdata.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "windows.h"
#include "winsock2.h"
#include "downdata.h"
#include "freeptr.h"
#pragma comment(lib, "ws2_32.lib") //在某些编译器下,需要添加编译参数到工程设置中,比如CodeBlocks(这样,这行就不需要了)。
//Code::blocks 中新建一个工程, 然后右击工程
//选择 build options
//选择Linker setting
//在 Other linker options 中添加: -lwsock32
/**
* @brief 为了加深http的理解和数据相关的操作,实现之。
*/
bool WEBQuest(const char * pUrl)
{
#if 1 //no this declaration, socket create failed.
WSADATA WsaData;
if (WSAStartup(MAKEWORD(2,2),&WsaData))
{
printf("The socket failed");
return false;
}
#endif
SOCKET sockeId;
SOCKADDR_IN addr;
if (-1 == (sockeId = socket(AF_INET, SOCK_STREAM, 0)))
{
printf("socket create failed\n");
return false;
}
//> dest_addr
addr.sin_addr.S_un.S_addr = inet_addr("74.125.71.105"); //> googleIP,可以用IP地址查询得到
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
//> request_url
/*
* 分离url中的主机地址和相对路径
*/
char *phost = 0;
char * myurl = (char * ) malloc (BUFFSIZE_H * sizeof(char));
char * host = (char * ) malloc (BUFFSIZE_H * sizeof(char));
char * GET = (char * ) malloc (BUFFSIZE_H * sizeof(char));
char * head =(char * ) malloc(BUFFSIZE_H * sizeof(char));
memset(myurl, '\0', BUFFSIZE_H);
memset(host, '\0', BUFFSIZE_H);
memset(GET, '\0', BUFFSIZE_H);
memset(head, '\0', BUFFSIZE_H);
//> pUrl -
//> - &host
//> /ig/api?hl=zh_cn&weather=beijing - &GET
strcpy(myurl, pUrl);
for (phost = myurl; *phost != '/' && *phost != '\0'; ++phost);
if ( (int)(phost - myurl) == strlen(myurl) )
strcpy(GET, "/");
else
strcpy(GET, phost);
*phost = '\0'; //> 相当于在/的/位置替换为'\0'.
strcpy(host, myurl);//> 在的/位置为'\0',因此被拷贝到&host.
printf("%s\n%s\n", host, GET);
/*
* 组织发送到web服务器的信息
* 为何要发送下面的信息connect请参考HTTP协议的约定
*/
strcat(head, "GET ");
strcat(head, GET);
strcat(head, " HTTP/1.1\r\n");
strcat(head, "host: ");
strcat(head, host);
strcat(head, "\r\nConnection: Close\r\n\r\n");
printf(head);
if (SOCKET_ERROR == connect(sockeId, (SOCKADDR * )&addr, sizeof(addr)))
{
printf("connect failed!\n");
closesocket(sockeId);
WSACleanup();
#if 0
free(myurl);
free(host);
free(GET);
free(head);
myurl = NULL;
host = NULL;
GET = NULL;
head = NULL;
#endif
freeCharPtr(&myurl, &host, &GET, &head, NULL);
return false;
}
if (SOCKET_ERROR == send(sockeId, head, strlen(head), 0))
{
printf("send &header error!\n");
closesocket(sockeId);
WSACleanup();
#if 0
free(myurl);
free(host);
free(GET);
free(head);
myurl = NULL;
host = NULL;
GET = NULL;
head = NULL;
#endif
freeCharPtr(&myurl, &host, &GET, &head, NULL);
return false;
}
memset(head, '\0', BUFFSIZE_H);
FILE *fp;
fp = fopen("whether.xml", "w+");
if (NULL == fp)
{
printf("whether file create failed!\n");
closesocket(sockeId);
WSACleanup();
#if 0
free(myurl);
free(host);
free(GET);
free(head);
myurl = NULL;
host = NULL;
GET = NULL;
head = NULL;
#endif
freeCharPtr(&myurl, &host, &GET, &head, NULL);
return false;
}
while (recv(sockeId, head, BUFFSIZE_H, 0) > 0)
{
fputs(head, fp);
memset(head, '\0', BUFFSIZE_H);
}
#if 0
free(myurl);
free(host);
free(GET);
free(head);
myurl = NULL;
host = NULL;
GET = NULL;
head = NULL;
#endif
freeCharPtr(&myurl, &host, &GET, &head, NULL);
closesocket(sockeId);
WSACleanup();
return true;
}
//> main.cpp
#include <iostream> //> system
#include <stdio.h>
#include "downdata.h"
using namespace std;
int main(void )
{
if (!WEBQuest("" ))
{
return -1;
}
printf( "Whether have been writtend to file whether.xml\n" );
system( "pause");
return 0;
}
//> 关于内存释放,自己封装了一个变参形式的(char *参数)释放函数
//> freeptr.h
#ifndef FREEPTR_H
#define FREEPTR_H
#ifdef __cpluscplus
extern "C"
{
#endif
void freeCharPtr(char ** ch, …);
#ifdef __cpluscplus
}
#endif
#endif
//> freeptr.cpp
#include <stdlib.h>
#include <stdarg.h>
#include "freeptr.h"
void freeCharPtr(char ** ch, …)
{
va_list ap;
char ** p;
va_start(ap, ch);
free(*ch);
*ch = NULL;
while (p = va_arg(ap, char ** ))
{
free(*p);
*p = NULL;
}
}
- C++实现Host请求_达内
- C堆内矩阵实现
- C++_递归实现汉诺塔
- C++_如何得到类内字符串内容
- 【GOF23设计模式】_中介者模式_同事协作类_内部类实现JAVA245
- Java设计模式_行为型_命令模式_请求与实现相分离
- 【面试】数据库部分_达内培训
- 多线程_匿名内部类的方式实现多线程程序
- CRC32的C行内汇编实现
- C内嵌汇编 实现write函数
- 内排序-数组实现(c++)
- 数据结构的C实现_图_邻接矩阵表示
- 数据结构的C实现_图_邻接表表示
- 动手实现Logistic Regression (c++)_测试_再次
- 数据结构的C实现_图_邻接表示
- C实现通过sock发送http请求
- C语言实现的http请求
- c语言实现的http请求
- Linux下软件安装方法总结
- 酷炫的卡片效果
- 项目时间(项目活动持续时间估算、计划编制)笔记
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance问题解决方法
- 物联网调查报告:机遇与挑战
- C++实现Host请求_达内
- 四十二 毕设(中) 我在软件园的那些日子里
- 更改WebStrom主题字体
- Dirichlet Process and Hierarchical Dirichlet Process
- innerHTML和html联系和区别
- ZIP压缩算法详细分析及解压实例解释
- python 2.7.x 和 3.x 版本区别
- vijos1070_关于次小生成树的求法
- CSS:class选择器的使用