Thread: C++ winsock downloading webpage 505 error

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    10

    C++ winsock downloading webpage 505 error

    Hi,
    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
    This is an example of the response:
    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
    I have noticed that if i'm getting the page from listings.ebay.co.uk it works but cgi.ebay.co.uk does not.

    Any help would be much appreciated.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    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)

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #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;
    	
    }

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Salem
    > 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
    That's because it's "WireShark" actually. It's either just a project rename or an actual fork, but essentially Ethereal has been replaced by WireShark.
    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

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    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 + ...
    I tried installing WireShark once, and it crashed or aborted on me... it installed ok this time, but now refuses to start. (Not to mention that WireShark is like twice the size of Ethereal.) I guess I'll reboot, to see it that fixes it.
    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)

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  9. #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

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Salem
    > 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
    The ethereal project changed it's name to wireshark, and the ethereal site is no longer updated!!!
    "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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM