http 协议 c++代码 获取网页
最近接触了些 html 和 JavaScript,索性了解下 http 协议。在园子里找到了 HTTP协议详解,图文并茂,很爽!
于是小小的尝试了下
#include <WinSock2.h>
#include <iostream>
#include <string>
#include <fstream> #pragma comment(lib, "ws2_32.lib") using namespace std; const int g_nPort = 80;
const string g_strAddr = "www.cnblogs.com";
const string g_strPath = "/hangj/"; int main(int argc, char* argv[])
{
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,2), &WSAData)){
printf("initializationing error!\n");
WSACleanup();
exit(-1);
} struct hostent *he;
if ((he = gethostbyname(g_strAddr.c_str()))==NULL){
printf("gethostbyname failed.\n");
WSACleanup();
exit(-1);
} struct sockaddr_in CliAddr = {0};
CliAddr.sin_family = AF_INET;
CliAddr.sin_port = htons(g_nPort);
CliAddr.sin_addr = *((struct in_addr*)he->h_addr_list[0]);
cout <<"ip: "<< inet_ntoa(*(struct in_addr*)he->h_addr_list[0]) <<endl; int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sockfd){
printf("socket failed.\n");
WSACleanup();
exit(-1);
}
if (0 > connect(sockfd, (struct sockaddr*)&CliAddr, sizeof(CliAddr))){
printf("connect failed.\n");
WSACleanup();
exit(-1);
}
string strSend="";
strSend += "GET " + g_strPath + " HTTP/1.1\n"; // URL
strSend += "Host: " + g_strAddr + "\n\n"; // Host char bufSend[1024]={0};
sprintf(bufSend, "%s", strSend.c_str());
if (0 > send(sockfd, bufSend, strlen(bufSend), 0)){
printf("send failed.\n");
WSACleanup();
exit(-1);
} ofstream os("hangj.html"); char bufRecv[10]={0};
int nRet=0;
printf("recv: \n");
while (sizeof(bufRecv)-1 <= (nRet=recv(sockfd, bufRecv, sizeof(bufRecv)-1, 0))){
bufRecv[nRet] = 0;
printf("%s", bufRecv);
os << bufRecv;
}
bufRecv[nRet] = 0;
printf("%s\n", bufRecv);
os << bufRecv; os.close();
closesocket(sockfd);
WSACleanup();
getchar();
return 0;
}
http 协议 c++代码 获取网页的更多相关文章
- 【转】如何在Windows+VS2005使用最新静态libcurl 7.35.0获取网页数据,支持HTTPS
地址: http://blog.csdn.net/hujkay作者:Jekkay Hu(34538980@qq.com)关键词:Windows,curl,ssl, visual c++ 2005, ...
- JS远程获取网页源代码的例子
js代码获取网页源代码. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < ...
- php正则获取网页标题、关键字、网页描述代码
php正则获取网页关键字,代码如下: function get_keywords($html) { $html=strtolower($html); preg_match("@<hea ...
- php获取网页中图片并保存到本地的代码
php获取网页中图片并保存到本地的代码,将网页中图片保存本地文件夹: <?php /** * 获取网页中图片,并保存至本地 * by www.jbxue.com */ header(" ...
- Python获取网页html代码
获取网页html代码: import requests res = requests.get('https://www.cnblogs.com/easyidea/p/10214559.html') r ...
- JSON使用——获取网页返回结果是Json的代码
public String getWebData(String strUrl){ String json = null; try { URL url = new URL(strUrl); HttpUR ...
- c# 获取网页源代码(支持cookie),最简单代码
/// /// 获取网页源码 public static string GetHtmls(string url, string referer = "", string cooki ...
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...
随机推荐
- DNA甲基化研究概述
DNA甲基化研究概述 生信技能树 已关注 2018.01.23 11:43 字数 993 阅读 183评论 0喜欢 1 DNA甲基化(DNA methylation)是最早被研究的重要表观遗传修饰之一 ...
- wpf 进度条 下拉
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsof ...
- Perl的调试模式熟悉和应用
perl -d file.pl perl -c file.pl DB<1> hList/search source lines: Control script ...
- 【转载】foreach+Control.Controls无法一次性移除所有子控件解决方法
博客转载地址:http://www.mzwu.com/article.asp?id=2254 //在panel1中添加20个Button ; ; ; i <= ; i++) { ) row++; ...
- 2018.09.15 poj1734Sightseeing trip(floyd求最小环)
跟hdu1599差不多.. 只是需要输出方案. 这个可以递归求解. 代码: #include<iostream> #include<cstdio> #include<cs ...
- git将本地仓库强制替换掉远程仓库
$ git remote add origin <url> $ git push --force --set-upstream origin master
- maven插件mybatis-generator生成代码
第一步,在pom中加入插件 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId> ...
- PHP中的mb_convert_encoding与iconv函数介绍
php传输乱码 mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍了. 不过英文一般不会存在编码问题,只有中文数据才会有这个问题.比 ...
- html使用技巧
line-height: 27px; /* lineheight和height保持一致就能达到文本垂直居中*/ .top_content li { list-style-image: url(../ ...
- #include 和 #import 的区别, @class 的含义
#import 和 #include 会包含这个类的所有信息,包括实体变量和方法 而#include比起 #import的好处不会引起重复包含 @class是用来做类引用的 @class就是告诉编译 ...