Re: Http winsock problem
Send the "Host: " header too. Multi-homed servers, that ordinarily expect
HTTP/1.1 requests, often cannot figure out HTTP/1.0 requests unless the
request also includes the "Host:"
<raj.rr7@gmail.com> wrote in message
news:1171374877.573390.101140@a34g2000cwb.googlegroups.com...
Hi,
I am creating a Http client application using winsock, evc++ 4.0 and K-
Jam (WIndows Mobile 5) that will get data from Http server. I am using
the following code.
WSADATA wsaData;
CString csErr;
memset(&wsaData, 0, sizeof(WSADATA));
if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
{
AfxMessageBox(_T("WSAStartup Error"));
return;
//return FALSE;
}
// Create a connection-oriented socket
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check to see if we have a valid socket
if(s == INVALID_SOCKET)
{
//AfxMessageBox(_T("Error Invalid Socket"));
int iSocketError = WSAGetLastError();
csErr.Format(_T("Socket Error :: %d"),iSocketError);
AfxMessageBox(csErr);
return;
//return FALSE;
}
// Get the host information
HOSTENT *hostServer = gethostbyname("www.xxxxxx.com");
if(hostServer == NULL)
{
int iSocketError = WSAGetLastError();
csErr.Format(_T("GetHostByName Error :: %d"),iSocketError);
AfxMessageBox(csErr);
//AfxMessageBox(_T("Error Get Host By Name"));
return;
//return FALSE;
}
// Set up the target device address structure
SOCKADDR_IN sinServer;
memset(&sinServer, 0, sizeof(SOCKADDR_IN));
sinServer.sin_family = AF_INET;
sinServer.sin_port = htons(80);
sinServer.sin_addr = *((IN_ADDR *)hostServer->h_addr_list[0]);
// Connect
if(connect(s, (SOCKADDR *)&sinServer, sizeof(sinServer)) ==
SOCKET_ERROR)
{
int iSocketError = WSAGetLastError();
csErr.Format(_T("Connect Error :: %d"),iSocketError);
AfxMessageBox(csErr);
return;
//return FALSE;
}
// Send a request to the server
char cBuffer[1024] = "";
int nBytesSent = 0;
int nBytesIndex = 0;
// Set up the buffer to send
//sprintf(cBuffer, "GET / HTTP/1.0\r\n\r\n");
sprintf(cBuffer, "GET /activation/activate.php?Version=3.0b HTTP/1.0\r
\n\r\n");
int nBytesLeft = strlen(cBuffer);
// Send the entire buffer
while(nBytesLeft > 0)
{
nBytesSent = send(s, &cBuffer[nBytesIndex], nBytesLeft, 0);
if(nBytesSent == SOCKET_ERROR)
{
AfxMessageBox(_T("Error Sending Data"));
break;
}
csErr.Format(_T("Data Send :: %d"),nBytesSent);
AfxMessageBox(csErr);
// See how many bytes are left. If we still need to send, loop
nBytesLeft -= nBytesSent;
csErr.Format(_T("Sending Bytes Left :: %d"),nBytesLeft);
AfxMessageBox(csErr);
nBytesIndex += nBytesSent;
}
// Get the response
TCHAR tchResponseBuffer[1024] = TEXT("\0");
char cResponseBuffer[1024] = "";
BOOL fBreak = FALSE;
int nBytesReceived = 0;
while(!fBreak)
{
nBytesReceived = recv(s, &cResponseBuffer[0], 1024, 0);
if(nBytesReceived == SOCKET_ERROR)
{
AfxMessageBox(_T("Error Receiving Data"));
break;
}
csErr.Format(_T("Bytes Received :: %d"),nBytesReceived);
AfxMessageBox(csErr);
//AfxMessageBox(_T("Data Received Successfully"));
// Convert the data from ANSI to Unicode
mbstowcs(tchResponseBuffer, cResponseBuffer, nBytesReceived);
// Show the MessageBox
AfxMessageBox(tchResponseBuffer);
// Check to see if this is the end of the HTTP response by
// looking for \r\n\r\n
if(_tcsstr(tchResponseBuffer, TEXT("\r\n\r\n")))
fBreak = TRUE;
// Clear the buffers
memset(tchResponseBuffer, 0, 1024);
memset(cResponseBuffer, 0, 1024);
}
closesocket(s);
WSACleanup();
Everything works fine but i get 500 Internal server error. Can anybody
has faced the same issue? Is that due to the HTTP/1.0 version or is it
a PHP proble?
Regards
Rajat