用C socket http发送这段请求报文

HTTP分为请求行,请求头部以及请求内容
那个长度数字值就是请求内容的长度(字节为单位)
给一段例子你就基本明白了:
onst CString getString(int i)
{
    CString s;
    s.Format(_T("%d"), i);
    return s;
}

CString cnt;
        CString psr;
        cnt.Append(_T("<?xml version=/"1.0/" encoding=/"utf-8/"?>/r/n"));
        cnt.Append(_T("<soap12:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-

instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/"

xmlns:soap12=/"http://www.w3.org/2003/05/soap-envelope/">/r/n"));
        cnt.Append(_T("<soap12:Body>/r/n"));
        cnt.Append(_T("<CelsiusToFahrenheit xmlns=/"http://tempuri.org//">/r/n"));
        cnt.Append(_T("<Celsius>100</Celsius>/r/n"));
        cnt.Append(_T("</CelsiusToFahrenheit>/r/n"));
        cnt.Append(_T("</soap12:Body>/r/n"));
        cnt.Append(_T("</soap12:Envelope>/r/n/r/n"));

        psr.Append(_T("POST "));
        psr.Append(_T("/webservices/tempconvert.asmx"));
        psr.Append(_T(" HTTP/1.1/r/nHOST: "));
        psr.Append(_T("www.w3schools.com"));
        psr.Append(_T("/r/nContent-Type: application/soap+xml; charset=utf-8"));
        psr.Append(_T("/r/nContent-Length: "));
        psr.Append(getString(CStringA(cnt).GetLength()));
        psr.Append(_T("/r/n/r/n"));
        psr.Append(cnt);

        CStringA szSendBuff(psr);
        int ntetv = send(m_sock, szSendBuff, szSendBuff.GetLength(), 0);
        ntetv = GetLastError();

第二个例子:
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32")

char *PisDatas1="POST /axis/services/AuthClient HTTP/1.1/r/n"
"Host: *.*.*.*:8080/r/n"
"Connection: Keep-Alive/r/n"
"User-Agent: EasySoap++/0.6/r/n"
"Content-Type: text/xml; charset=/"UTF-8/"/r/n"
"SOAPAction: /"http://DefaultNamespace#m_string_1/"/r/n";
//"Content-Length: 697/r/n/r/n";
char *PisDatas2=
"<E:Envelope/r/n"
"    xmlns:E=/"http://schemas.xmlsoap.org/soap/envelope//"/r/n"
"    xmlns:A=/"http://schemas.xmlsoap.org/soap/encoding//"/r/n"
"    xmlns:s=/"http://www.w3.org/2001/XMLSchema-instance/"/r/n"
"    xmlns:y=/"http://www.w3.org/2001/XMLSchema/"/r/n"
"    E:encodingStyle=/"http://schemas.xmlsoap.org/soap/encoding//">/r/n"
"<E:Body>/r/n"
"<m:m_string_1/r/n"
"    xmlns:m=/"http://DefaultNamespace/">/r/n"
"<m:in0/r/n"
"    s:type=/"y:string/">8080</m:in0>/r/n"
"<m:in1/r/n"
"    s:type=/"y:string/">360781102</m:in1>/r/n"
"<m:in2/r/n"
"    s:type=/"y:string/">rt9188</m:in2>/r/n"
"<m:in3/r/n"
"    s:type=/"y:string/">select * from admin</m:in3>/r/n"
"<m:in4/r/n"
"    s:type=/"y:string/">gettable_file</m:in4>/r/n"
"</m:m_string_1>/r/n"
"</E:Body>/r/n"
"</E:Envelope>/r/n";

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD(1,1);

err = WSAStartup( wVersionRequested, &wsaData );
if( err != 0)
{
return ;
}
if( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
WSACleanup();
return ;
}

SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("*.*.*.*");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8080);

connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//printf("%d/n",strlen(PisDatas2));
char SendDatas[1024];
sprintf(SendDatas, "%sContent-Length: %d/r/n/r/n%s", PisDatas1,strlen(PisDatas2),PisDatas2);
printf("%s",SendDatas);
send(sockClient,SendDatas,strlen(SendDatas),0);
char recvBuf1[1024];
recv(sockClient,recvBuf1,1024,0);
char recvBuf2[4096];
recv(sockClient,recvBuf2,4096,0);
printf("%s/n",recvBuf1);
printf("%s/n",recvBuf2);

closesocket(sockClient);
WSACleanup();
}

未通过测试.
摘自:http://topic.csdn.net/u/20100322/17/7349580c-ce2d-4c7a-8c3d-d337a5ce9e93.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值