怎么在c语言中用socket的方式调用webservice
2017-08-08 · 威鼎触摸一体机液晶拼接屏广告机
深圳威鼎科技有限公司
威鼎科技专注于视觉图像视频、多媒体领域软件的研发,以及交互式多媒体触摸一体机,智能教学、电子白板触控一体机及设备,会议平板,液晶拼接屏,矩阵,液晶拼接电视墙,液晶广告机的设计生产,研发与服务
向TA提问
关注
展开全部
可以实现,先connect
sockaddr_in server;
if (m_bSockConnected)
{
return TRUE;
}
server.sin_addr.s_addr = inet_addr(m_szHost);
server.sin_family = AF_INET;
server.sin_port = htons((UINT)m_nPort);
if(connect(m_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
m_bSockConnected = FALSE;
m_nLastErrorNo = WSAGetLastError();
MessageBox(0, "请检网络和服务器", "网络连接错误", 0);
return FALSE;
}
else
{
m_bSockConnected = TRUE;
}
int timeout=30;
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(int));
然后再发送url
char requestHeader[1024]= {0};
char nBuf[36] = {0};
strcpy(requestHeader, "PUT ");
strcat(requestHeader, szUrl);
strcat(requestHeader, "&Content-Length=");
_ui64toa(contentLength + 8, nBuf,10); //额外加了传输完成头的长度是8 strlen(SEND_END_STRING)
strcat(requestHeader, nBuf);
strcat(requestHeader, "&FileCount=");
ltoa(fileCount,nBuf, 10);
strcat(requestHeader, nBuf);
strcat(requestHeader," HTTP/1.1\r\nContent-Length: ");
_ui64toa(contentLength + 8, nBuf, 10);
strcat(requestHeader,nBuf);
strcat(requestHeader,"\r\nHost: ");
strcat(requestHeader,m_szHost);
strcat(requestHeader,":");
itoa(m_nPort,nBuf,10);
strcat(requestHeader,nBuf);
strcat(requestHeader,"\r\nAccept: */*;\r\nAccept-Encoding: gzip,deflate\r\nKeep-Alive: 60000\r\nConnection: keep-alive\r\n\r\n");
//MessageBox(0, requestHeader, "info", 0);
char * szUTF8 = NULL;
BOOL bRet = FALSE;
bRet = CCharTransLib::EncodeToUTF8(requestHeader,&szUTF8);
if (bRet)
{
if ( (int)strlen(szUTF8) != send(m_socket, szUTF8, strlen(szUTF8), 0))
{
m_nLastErrorNo = GetLastError();
bRet = FALSE;
}
if (szUTF8)
{
delete []szUTF8;
szUTF8 = NULL;
}
}
以上是从我工程里面摘的,可能有些变量楼主编译不过去,但基本思想就是这样的
sockaddr_in server;
if (m_bSockConnected)
{
return TRUE;
}
server.sin_addr.s_addr = inet_addr(m_szHost);
server.sin_family = AF_INET;
server.sin_port = htons((UINT)m_nPort);
if(connect(m_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
m_bSockConnected = FALSE;
m_nLastErrorNo = WSAGetLastError();
MessageBox(0, "请检网络和服务器", "网络连接错误", 0);
return FALSE;
}
else
{
m_bSockConnected = TRUE;
}
int timeout=30;
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(int));
然后再发送url
char requestHeader[1024]= {0};
char nBuf[36] = {0};
strcpy(requestHeader, "PUT ");
strcat(requestHeader, szUrl);
strcat(requestHeader, "&Content-Length=");
_ui64toa(contentLength + 8, nBuf,10); //额外加了传输完成头的长度是8 strlen(SEND_END_STRING)
strcat(requestHeader, nBuf);
strcat(requestHeader, "&FileCount=");
ltoa(fileCount,nBuf, 10);
strcat(requestHeader, nBuf);
strcat(requestHeader," HTTP/1.1\r\nContent-Length: ");
_ui64toa(contentLength + 8, nBuf, 10);
strcat(requestHeader,nBuf);
strcat(requestHeader,"\r\nHost: ");
strcat(requestHeader,m_szHost);
strcat(requestHeader,":");
itoa(m_nPort,nBuf,10);
strcat(requestHeader,nBuf);
strcat(requestHeader,"\r\nAccept: */*;\r\nAccept-Encoding: gzip,deflate\r\nKeep-Alive: 60000\r\nConnection: keep-alive\r\n\r\n");
//MessageBox(0, requestHeader, "info", 0);
char * szUTF8 = NULL;
BOOL bRet = FALSE;
bRet = CCharTransLib::EncodeToUTF8(requestHeader,&szUTF8);
if (bRet)
{
if ( (int)strlen(szUTF8) != send(m_socket, szUTF8, strlen(szUTF8), 0))
{
m_nLastErrorNo = GetLastError();
bRet = FALSE;
}
if (szUTF8)
{
delete []szUTF8;
szUTF8 = NULL;
}
}
以上是从我工程里面摘的,可能有些变量楼主编译不过去,但基本思想就是这样的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询