![]() |
| | #1 |
| Registered User Join Date: Mar 2006
Posts: 10
| C++ winsock downloading webpage 505 error I been playing around with C++ using winsock.h to download webpages. I can get it to work for pages such as http://www.research.att.com/~bs/homepage.html I have also tried for ebay, some pages work fine, however pages like ebay example don't work, and i get a 505 error code. From what i understand this code means that there is an incompatibility between the HTTP versions of the client and server. However this does not seem to be the case since I send that I support 1.1, and the server sends a reply that it supports 1.1. This is an example of the message i send: Code: GET windows-folder_W0QQitemZ280062863525QQihZ018QQcategoryZ80555QQrdZ1QQcmdZViewItem HTTP/1.1 Host: cgi.ebay.co.uk Code: HTTP/1.1 505 HTTP Version Not Supported Server: Apache-Coyote/1.1 Date: Fri, 29 Dec 2006 00:29:55 GMT Connection: close Any help would be much appreciated. |
| god_of_war is offline | |
| | #2 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| As much as I love to divine answers, actual code does help considerably. The page in question on ebay does support HTTP/1.1, as that's what Firefox uses to get the page. Also, have you checked what actually what gets sent from your program with a network analyzer like Ethereal?
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #3 |
| * Death to Visual Basic * Join Date: Aug 2001
Posts: 768
| The only good way to develope network applications is with a sniffer. Just one thing, Ethereal is DEAD... It is now known as WireShare.org
__________________ "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe http://www.Bloodware.net - Developing free software for the community. |
| Devil Panther is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,680
| > Just one thing, Ethereal is DEAD... It is now known as WireShare.org WTF are you talking about? http://www.ethereal.com/ is alive and well Your other site comes up 404 |
| Salem is offline | |
| | #5 |
| Registered User Join Date: Mar 2006
Posts: 10
| Thanks for the replies. The code has worked for multiple different webpages, but seems to have a problem with pages on cgi.ebay.co.uk I have never used a network analyser before, I will take a look at Ethereal now. Here's the code I have been using, which gets the header of a page using a HEAD request. (I have been using GET to get the page). If anyone can spot anything causing the problem that would be great. (I have copied the code exactly from a working program so it should compile fine). libws2_32.a is linked to the project. Code: #include <winsock.h>
#include <iostream>
int GetHTTP(std::string lpServerName, std::string lpFileName, std::string* file);
int main()
{
std::string page_string;
//int GetHTTP_return = GetHTTP("www.research.att.com", "/~bs/homepage.html" , &page_string);
int GetHTTP_return = GetHTTP("cgi.ebay.co.uk", "/windows-folder_W0QQitemZ280062863525QQihZ01"
"8QQcategoryZ80555QQrdZ1QQcmdZViewItem" , &page_string);
if (GetHTTP_return == 0)
{
std::cout << "Header received. \n\n" << page_string << std::endl;
}
else std::cout << "ERROR" << std::endl;
system ("PAUSE");
return 0;
}
int GetHTTP(std::string lpServerName, std::string lpFileName, std::string* file)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
nRet = WSAStartup(wVersionRequested, &wsaData);
if (nRet != 0)
{
WSACleanup();
return 1;
}
if (wsaData.wVersion != wVersionRequested)
{
WSACleanup();
return 2;
}
IN_ADDR iaHost;
LPHOSTENT lpHostEntry;
iaHost.s_addr = inet_addr(lpServerName.c_str());
if (iaHost.s_addr == INADDR_NONE)
{
lpHostEntry = gethostbyname(lpServerName.c_str());
}
else
{
lpHostEntry = gethostbyaddr((const char *)&iaHost,
sizeof(struct in_addr), AF_INET);
}
if (lpHostEntry == NULL)
{
return WSAGetLastError();
}
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET)
{
return WSAGetLastError();
}
LPSERVENT lpServEnt;
SOCKADDR_IN saServer;
lpServEnt = getservbyname("http", "tcp");
if (lpServEnt == NULL)
saServer.sin_port = htons(80);
else
saServer.sin_port = lpServEnt->s_port;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
closesocket(Socket);
return WSAGetLastError();
}
std::string szBuffer_s;
szBuffer_s = "HEAD " + lpFileName + " HTTP/1.1 \r\nHost: " + lpServerName +
"\r\nConnection: close" "\r\n\r\n";
std::cout << "Client message sent. \n\n" << szBuffer_s << std::endl;
nRet = send(Socket, szBuffer_s.c_str(), szBuffer_s.length(), 0);
if (nRet == SOCKET_ERROR)
{
closesocket(Socket);
return WSAGetLastError();
}
char szBuffer[1024];
int array_int_copy = 0;
while(1)
{
nRet = recv(Socket, szBuffer, 1024, 0);
if (nRet == SOCKET_ERROR)
{
closesocket(Socket);
return WSAGetLastError();
}
if (nRet == 0)
break;
array_int_copy = 0;
while (array_int_copy != nRet)
{
*file += szBuffer[array_int_copy];
array_int_copy+=1;
}
}
closesocket(Socket);
WSACleanup();
return 0;
}
|
| god_of_war is offline | |
| | #6 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #7 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| Removing the space after "HTTP/1.1" works for me. (No space between HTTP/1.1 and the \r\n) Literally: Code: szBuffer_s = "HEAD " + lpFileName + " HTTP/1.1\r\nHost: " + lpServerName + ...
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #8 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| The shark works flawlessly for me. But perhaps the Windows version is less stable.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #9 |
| Registered User Join Date: Mar 2006
Posts: 10
| Cactus_Hugger you are a genius. Worked like a charm. Simple problems are always the most frustrating. Thanks for all the help. god_of_war |
| god_of_war is offline | |
| | #10 | |
| * Death to Visual Basic * Join Date: Aug 2001
Posts: 768
| Quote:
__________________ "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe http://www.Bloodware.net - Developing free software for the community. | |
| Devil Panther is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Errors including <windows.h> | jw232 | Windows Programming | 4 | 07-29-2008 01:29 PM |
| file reading | gunghomiller | C++ Programming | 9 | 08-07-2007 10:55 PM |
| Post... | maxorator | C++ Programming | 12 | 10-11-2005 08:39 AM |
| Learning OpenGL | HQSneaker | C++ Programming | 7 | 08-06-2004 08:57 AM |
| Stupid compiler errors | ChrisEacrett | C++ Programming | 9 | 11-30-2003 05:44 PM |