`

一个简单的httpServer(STL编写,控制台程序)

阅读更多

从codeproject上找来,链接:http://www.codeproject.com/KB/IP/socketserver.aspx

A set of classes written in STL that implement a web server

Introduction

  • This article presents code for a web server implemented using sockets and STL. The code for the web server was adapted from the httpsvr sample in MSDN. The visual interface has been removed. All of the the MFC code has been ripped out and converted to STL, and socket classes were implemented.

    The socket classes are:

    • Socket
    • ServerSocket

    These feed handler classes:

    • SocketHandler
    • ServerSocketHandler
    • HttpSocketHander

    Socket notification comes from the abstract class SocketNotify which SocketHandler implements. A reference can be passed in to override the notification.

    The http request classes are:

    • HttpBuffer
    • HttpRequest
    • HttpRquestProcess
    • HttpResponse

    The request class allows you to create requests or to store a request. The request process processes requests by passing in a pointer to a buffer. The request process class gets initial info like the URL and then forms a response.

    The startup code is in Server.cpp which declares an instance of HttpServer. To set up the code to run set the directory where your web pages are and then compile and run.

  • 此程序是用来作为测试的代码,看到是用stl写的,感觉写的挺简洁的,只是在调试的时候,发现对于vs2005还需要做稍许修改才能编译通过,稍后会发到资源中去。

  • 与之相对应的,是一个http数据发送器,用来测试此httpserver.同样来源于

    http://www.codeproject.com/KB/IP/httpget-post.aspx

  • This article presents a utility that lets you retrieve raw information from web servers using HTTP's GET and POST commands.

    Description

    This utility is just a wrapper around reusable functions that allow programmatic access to the web through a sort of 'mini-browser' embedded inside your program.

    There are many uses for such code. Programs that look at a series of web pages, much like a user surfing from one page to the next, are often called spiders, bots, or crawlers. Such programs are often used to catalog websites, import external data from the Web, or simply to send commands to a web server. You could extend the functionality of the classes presented here to retrieve information from the Internet in a variety of ways.

    There are many third-party DLLs and solutions which retrieve data from websites. The functions presented in this article are totally self-contained. There is no reliance on WinInet, Internet Explorer, Netscape, or any requirement that similar software be installed, apart from WinSock. WinSock is an integral part of the Windows TCP/IP stack and is available on any computer capable of running a browser.

    Every Internet protocol is documented in an RFC (Request For Comments) document. HTTP is documented in RFC1945. Additionally, RFC1630, RFC1708 and RFC1808 document the format of a URL.

    A complete set of RFCs can be found at http://www.rfc-editor.org.

    Implementation

    The engine of the utility is in the Request class. The key function is SendHTTP(). This function accepts 5 parameters and returns one integer. The first parameter is the URL to POST to or GET from. The second parameter specifies any additional HTTP headers to be passed during this request. The third and fourth parameters specify the data and length of data to post. The fifth parameter is a pointer to an HTTPRequest structure that will hold the headers and messages sent and returned by the web server. SendHTTP return 0 if the POST or GET was succesful, otherwise 1 to indicate an error.

    SendHTTP() begins by parsing the URL string. A URL is an address that specifies the exact location of a resource on the Internet. A URL has several parts, some of which are optional. An example of a URL would be:

    http://www.codetools.com:80/index.html

    The first part of the URL is the protocol which specifies how to receive the resource. Following the protocol is the host name. This can be either a domain name or an IP address. Following the host is a port number. Every protocol has a default port number to be used if no port is specified. The default HTTP port is port 80. Following the port is the request being made of the specified web server. If not specified, it defaults to just '/', which requests the root document of the web server.

    Next, SendHTTP() initializes the WinSock library by calling WinSock's WSAStartup(). After establishing a socket connection, SendHTTP() transmits a request to the server. There are 2 forms of HTTP requests. The first, and simpler form, is the HTTP GET.

    An HTTP GET does not send any additional information to the web server other than the request headers and the URL. An HTTP GET often uses the URL itself to send additional information:

    http://localhost/projects/HTTP/TestGet.asp?name=fred&age=22

    The second form, an HTTP POST, sends data along with the request, separate from the URL.

    Usually, an HTTP POST include the header:

    Content-Type:application/x-www-form-urlencoded

    Without this header, some web servers (particularly ASP running on IIS) will not recognize your parameters. An HTTP POST has 2 parts. The first is the HTTP headers, just as in the GET. The headers contain the actual request and additional pieces of information. Unlike a GET, a POST contains data after the headers (separated from them by a blank line).

    After the web server receives the GET or POST request, it sends back a response. The response has 2 parts: headers followed by data (with a blank line separating the two).

    The first line of the HTTP headers specifies the status of the request. It starts with a numeric error code.

    100-199 is an informational message and is not generally used.
    200-299 means a successful request.
    300-399 indicates that the requested resource has been moved; web servers use this for redirection.
    400-499 indicates client errors.
    500-599 indicates server errors.

    After the headers comes the data returned by the GET or POST request. This is usually seen on the browser screen.

    Dialog box wrapper

    The MFC dialog project is used like a wrapper to the Request class. In the dialog container is inserted a instance of the Microsoft Web Browser control. This makes it very easy to navigate the data, make commands like GET or POST. The control is used in 2 ways:

    1. When the user makes a request from the browser, the control fires the OnBeforeNavigate2 event which is captured by the dialog program. In that way, in OnBeforeNavigate2Explorer1 function is used to discover if is a GET or POST, the header sent to the web server and the posted data.

    2. If the user wants to use the SendHTTP engine, enter the required URL, complete the 'SendHTTPrequest' and 'PostData' (if is a POST) fields, chack the radio button GET or POST and click on the 'Go' button. The IE control will load the HTML formatted data received from SendHTTP() function in the m_HTTPbody string variable. The HTML loading is done in OnButtonViewHttp().

    Collapse | Copy Code

    IHTMLDocument2* pHTMLDocument2;
    LPDISPATCH lpDispatch;
    lpDispatch = m_Browser.GetDocument();
    
    if (lpDispatch)
    {
        HRESULT hr;
        hr = lpDispatch->QueryInterface(IID_IHTMLDocument2,
                                        (LPVOID*)&pHTMLDocument2);
        lpDispatch->Release();
        IHTMLElement* pBody;
        hr = pHTMLDocument2->get_body(&pBody);
    
        BSTR bstr;                
        bstr = m_HTTPbody.AllocSysString();
    		
        pBody->put_innerHTML(bstr);	//insert the html
    	   
        SysFreeString(bstr);
        pBody->Release();
    }

    Usage

    Input the URL address and click on the Go button. On the right there is a mini-browser with your page. Navigating on links and buttons on this page and in the 'PostData', 'SendHTTPrequest' and 'ReceiveHTTPrequest' will receive the corresponding data. The radio buttons Get/Post are modified automatically - the IE instance knows if you make an GET (you push on a link) or POST (you push a button).

    You are able to input your header in the 'SendHTTPrequest' edit box and your POST data in the 'PostData' edit box, and then push the 'Go' button. The browser will navigate to your address using the headers and data submitted from 'SendHTTPrequest' and 'PostData' fields.

    Use the TestGet.asp and TestPost.asp files from Web directory to test your GET/POST utility :

    分享到:
    评论

    相关推荐

      杨辉三角(C++语言控制台程序)

      **杨辉三角**,又称帕斯卡三角,是数学中一个非常重要的概念,它在组合数学、概率论以及计算机科学中都有广泛的应用。这个三角形的每一行都是一个数列,其中每个数字是上一行相邻两个数字的和。在C++编程中实现杨辉...

      STL模板c++学生成绩管理

      运用stl编写的学生管理系统,在百度上拿过来的~

      西门子内部教材-STL语言编写讲解

      西门子STL语言,全称Structured Text,是一种在PLC编程中常见的高级文本编程语言,主要用于西门子S7系列控制器。STL语言结构清晰,语法严谨,适合编写复杂的控制逻辑。以下是对STL语言及其相关知识点的详细说明: 1...

      控制台程序 八数码 代码 可执行程序

      总的来说,这是一个用VC++6.0编写的控制台程序,它实现了八数码游戏的解决方案,允许用户输入初始和目标状态,并输出解决问题的最小步数和过程。通过这样的程序,我们可以学习到如何利用计算机解决经典的逻辑问题,...

      西门子S7-300 STL编写PID例程(英文).pdf

      1. 例程概述:文档首先介绍了示例程序的功能,即如何使用S7-300 PLC的STL语言编写一个基本的PID控制器。 2. PID控制器的应用场景:文档解释了PID控制器的使用场合,包括它在工业控制中的普遍应用。 3. 自动模式与...

      STL 编写的CString 标准模板库

      然而,标题提到的是"STL编写的CString",这意味着我们将探讨一个与MFC中可能不同的实现,即使用标准模板库(STL)来创建类似于`CString`的功能。STL是C++的一个核心部分,提供了容器、迭代器、算法和函数对象等工具,...

      利用STL编写的INI文件读写类

      STL(Standard Template Library,标准模板库)是C++编程语言中的一个重要组成部分,它提供了一系列高效、可重用的数据结构和算法。在这个项目中,"利用STL编写的INI文件读写类"是为了方便地处理INI配置文件而设计的...

      3D 打印 stl 格式读取程序

      这个“3D 打印 stl 格式读取程序”是使用MATLAB编程语言编写的,用于读取和展示STL模型。MATLAB是一种强大的计算环境,适合进行数值分析、图像处理以及各种工程计算,包括3D图形处理。 STL文件格式由一系列三角面片...

      18个STL程序带你逐步了解STL

      STL是一个包含容器、迭代器、算法和函数对象的库,它通过模板类和函数模板实现了泛型编程。它的核心思想是分离接口和实现,使得程序员可以专注于问题的逻辑,而不是底层数据结构和算法的细节。 2. **容器**: - *...

      一个将灰度高度图图像转换为 STL 三角形网格的简单程序_c语言

      这个“一个将灰度高度图图像转换为STL三角形网格的简单程序”是用C语言编写的,目的是帮助用户将高度图转化为可打印的3D模型。 首先,我们要理解灰度高度图。这种图像通常由黑白像素组成,其中像素的灰度值代表对应...

      STL文档与简单的程序

      STL,全称为Standard Template Library(标准模板库),是C++编程语言中不可或缺的一部分,它为程序员提供了高效、灵活和可重用的容器...因此,对于任何想要深入掌握C++的程序员来说,STL都是一个必须学习的重要模块。

      stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层

      `stlread.m`可能是一个读取STL文件的MATLAB函数,而`ui.m`可能包含用户界面元素,用于交互式设定分层参数。 在MATLAB中,STL文件的读取通常涉及以下步骤: 1. 使用`stlread`函数:MATLAB社区或者自定义的`stlread.m...

      CT图像生成STL文件的程序

      这个"CT图像生成STL文件的程序"可能是一个专门用于处理医学影像数据,将其转换为3D可打印格式的应用。该程序可能具有以下功能: 1. 图像导入:支持导入不同格式的CT扫描图像,如DICOM(Digital Imaging and ...

      STL模型文件浏览程序

      Visual C++是一个强大的集成开发环境(IDE),常用于编写Windows平台上的应用程序,尤其是那些需要高性能计算或图形处理的项目,如CAD应用。 OpenGL是一个跨语言、跨平台的编程接口,用于生成2D和3D图形。在这个...

      STL入门 STL入门 STL入门 STL入门 STL入门 STL入门

      STL的出现是计算机科学抽象能力发展的一个里程碑,它引入了泛型编程的概念,允许程序员编写不依赖于具体数据类型的代码,从而实现更高的复用性和灵活性。 在STL中,有六个关键的组件: 1. **迭代器(Iterator)**:...

      STL.rar_3d_STL_STL文件显示_labview_stl顶点向量

      每个STL文件由一个头信息和一系列的三角形面片组成,每个面片包含3个顶点坐标和一个法向量,用于描述模型的表面朝向。 2. LabVIEW与3D图形: LabVIEW是一款强大的图形化编程环境,支持创建复杂的用户界面和数据...

      C++ stl版通讯录

      用stl编写的简易通讯录 ,可以实现简单的增添 删除 查找 等功能

      泛型程序设计与STL

      ### 泛型程序设计与STL:理解与应用 #### 泛型程序设计:概念与优势 泛型程序设计(Generic Programming)是一种编程范式,它允许程序员编写能够处理多种数据类型的代码,而无需为每种类型重复编写相同的逻辑。...

      STL程序源代码

      至于提供的部分内容,这部分看起来更像是一个建筑工程的预算列表,而非直接的STL程序源代码。它列出了各种区域的尺寸和计算出的面积,然后乘以单价得出总费用。虽然这不是STL编程的例子,但可以想象,如果要处理这样...

      STL文件的读取显示.zip_STL c++_STL文件_STL读取_读取stl_读取stl文件

      每个STL文件包含一个头信息和一系列的三角形面片描述。头信息通常为“solid”开头,然后是一些描述性的文字,接着是三角形面片的数据。 读取STL文件的过程通常包括以下步骤: 1. 打开文件:使用C++的fstream库打开...

    Global site tag (gtag.js) - Google Analytics