C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 12-28-2006, 06:37 PM   #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.
god_of_war is offline   Reply With Quote
Old 12-28-2006, 07:23 PM   #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)
Cactus_Hugger is offline   Reply With Quote
Old 12-29-2006, 01:37 AM   #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.
Devil Panther is offline   Reply With Quote
Old 12-29-2006, 02:25 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
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
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 12-29-2006, 06:11 AM   #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   Reply With Quote
Old 12-29-2006, 07:13 AM   #6
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
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
CornedBee is offline   Reply With Quote
Old 12-29-2006, 12:33 PM   #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)
Cactus_Hugger is offline   Reply With Quote
Old 12-29-2006, 12:40 PM   #8
Cat without Hat
 
CornedBee's Avatar
 
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   Reply With Quote
Old 12-29-2006, 02:50 PM   #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   Reply With Quote
Old 12-29-2006, 04:35 PM   #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.
Devil Panther is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22