我看了http请求包的格式,我想做一个程序向web服务器发送http请求包的内容.
包的格式就应该是:
get / http/1.1
accept: */*
…………
我想自己发送一些这样的字符串给web服务器,按照http请求的格式。
但是我能够从web服务器上收到发回的数据吗?

解决方案 »

  1.   

    当然可以
    //Get html page by SDK socket
    #define host_name "www.csdn.net"
    #define winsock_version 0x0101
    void main()
    {
        SOCKADDR_IN saServer;
    LPHOSTENT lphostent;
    WSADATA wsadata;
        SOCKET hsocket;
    int nRet;
        char hostname[100] ;
    wsprintf(hostname,"GET /expert/topic/378/378382.shtm HTTP/1.0 %c%c",10,10);
    printf("%s",hostname);
    char dest[1000];
    if(WSAStartup(winsock_version,&wsadata))
    printf("can't open");
        lphostent=gethostbyname(host_name);
        if(lphostent==NULL)
    printf("lphostent is null");
    hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        saServer.sin_family = AF_INET;
    // Use def. now, need to handle general case
    saServer.sin_port = htons(80);

    saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
        nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
    if (nRet == SOCKET_ERROR)
    {
    printf("can't connect");
    closesocket(hsocket);
    return;
    }
    else
    printf("connected with %s\n",host_name);
        nRet = send(hsocket, hostname, strlen(hostname), 0);
    if (nRet == SOCKET_ERROR)
    {
    printf("send() failed");
    closesocket(hsocket);

    }
    else
    printf("send() OK\n");
    nRet=1;
    while(nRet>0)
    {
    nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);
    if(nRet>0)
    dest[nRet]=0;
    else
    dest[0]=0;
    printf("\nReceived bytes:%d\n",nRet);
    printf("Result:\n%s",dest);
    }

    }///////////////////////////////////////////////////////////////////////////
    //SDK post
    ///////////////////////////////////////////////////////////////////////////
    #include "stdafx.h"
    #include "winsock.h"
    #pragma comment(lib,"ws2_32.lib")
    #define winsock_version 0x0101
    void main()
    {
    //I create  C:\Inetpub\wwwroot\test\test.asp ,start the web service
    //start my program, the result is OK.
    //If it works,it is written by masterz,otherwise I don't know who write it.
        SOCKADDR_IN saServer;
    LPHOSTENT lphostent;
    WSADATA wsadata;
        SOCKET hsocket;
    int nRet;
    const char* host_name="127.0.0.1";
    char* req="POST /test/test.asp HTTP/1.0\r\n"
    "From: local\r\n"
    "User-Agent: post_test/1.0\r\n"
    "Content-Type: application/x-www-form-urlencoded\r\n"
    "Content-Length: 20\r\n\r\n"
    "type=12345&name=aaaa";
    if(WSAStartup(winsock_version,&wsadata))
    printf("can't initial socket");
        lphostent=gethostbyname(host_name);
        if(lphostent==NULL)
    printf("lphostent is null");
    hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        saServer.sin_family = AF_INET;
    // Use def. now, need to handle general case
    saServer.sin_port = htons(80);
    saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
        nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
    if (nRet == SOCKET_ERROR)
    {
    printf("can't connect");
    closesocket(hsocket);
    return;
    }
    else
    printf("connected with %s\n",host_name);
    nRet = send(hsocket, req, strlen(req), 0);
    if (nRet == SOCKET_ERROR)
    {
    printf("send() failed");
    closesocket(hsocket);

    }
    else
    printf("send() OK\n");
    char dest[1000];
    nRet=1;
    while(nRet>0)
    {
    nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);
    if(nRet>0)
    dest[nRet]=0;
    else
    dest[0]=0;
    printf("\nReceived bytes:%d\n",nRet);
    printf("Result:\n%s",dest);
    }
    }