Trích dẫn Nguyên bản được gửi bởi RocK Xem bài viết
Google Translate

Demo:

Code:
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <winsock2.h>

#pragma comment (lib,"ws2_32.lib")

using namespace std;

#define WPORT 80
#define WHOST "translate.google.com.vn"

bool Startup  (SOCKET& sock);
bool Connect  (SOCKET& sock);
void Shutdown (SOCKET& sock);

std::vector<std::string> Tokenize(const std::string & str, const std::string & delim);

bool Startup(SOCKET& sock)
{
	DWORD dwError = 0;

	WSADATA wsaData = {0};

	dwError = WSAStartup(MAKEWORD(2,2), &wsaData);

	if(dwError != ERROR_SUCCESS)	
	{
		printf("Startup winsock fail !...\n");
		return 0;
	}

	printf("Startup winsock successful !...\n");
	sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	if (sock == INVALID_SOCKET)
	{
		printf("Start socket fail !...\n");
		WSAGetLastError();
		WSACleanup();
		return 0;
	}
	printf("Start socket successful\n");
	return 0;
}

bool Connect (SOCKET& sock)
{
	DWORD		dwError		= 0;
	hostent		*host		= 0;
	sockaddr_in srvHost		= {0};

	host = gethostbyname(WHOST);
	srvHost.sin_family		= AF_INET;
	srvHost.sin_port		= htons(WPORT);
	srvHost.sin_addr.s_addr = *((unsigned long*)host->h_addr);

	dwError = connect(sock, (struct sockaddr*)&srvHost, sizeof(srvHost));

	if(dwError)	
	{
		dwError = WSAGetLastError();
		printf("Connect to [%s:%d] fail !...\n",WHOST,WPORT);
		return 0;
	}
	printf("Connect to [%s:%d] successful !...\n",WHOST,WPORT);
	return 1;
}

void Shutdown(SOCKET& sock)
{
	if(sock)
	{
		closesocket(sock);
		printf("Close socket done !...\n");
	}
	WSACleanup();
	printf("Clear winsock done !...\n");
}

void main()
{
	SOCKET sock;
	Startup(sock);
	Connect(sock);

	string Line		= "";
	string Link		= "GET /translate_a/t?client=t&text=Hello&sl=en&tl=vi&pc=0&oc=1 HTTP/1.1\r\n";
	string Request	= "Host: translate.google.com.vn\r\n";
	string UserAgent= "User-Agent: Translate Client\r\n\n";

	char szLine[200] = {0};

	do
	{
		printf("Translate: ");
		scanf("%s",szLine);
		Link  = "GET /translate_a/t?client=t&text=";
		Link += szLine;
		Link += "&sl=en&tl=vi&pc=0&oc=1 HTTP/1.1\r\n";

		send(sock,Link.data()		,Link.length()		,0);
		send(sock,Request.data()	,Request.length()	,0);
		send(sock,UserAgent.data()	,UserAgent.length()	,0);

		bool done				= false;
		int	 EndHeader			= false;
		int  iByteRevc			= 0;
		char HTMLHeader			= 0;
		char HTMLResponse[5120] = {0};

		while(!done)
		{
			iByteRevc = recv(sock,&HTMLHeader,1,0);

			if(iByteRevc < 0) done = true;

			switch(HTMLHeader)
			{			
				case '\r':
					break;
				case '\n':
					if (EndHeader == true)
					{						
						done = TRUE;
					}
					EndHeader=true;
					break;
				default:
					EndHeader=false;
					break;
			} 
		}

		do
		{
			iByteRevc = recv(sock,HTMLResponse,sizeof(HTMLResponse)-1,0);
			if (HTMLResponse[0] == 0)
				HTMLResponse[0] = ' ';
			if(iByteRevc < 0)	break;
			HTMLResponse[iByteRevc] = 0;
		}
		while(0);

		std::vector<std::string> Token;
		Token = Tokenize(HTMLResponse,"[],\"");

		printf("--------------------------------------\n");
		for(int i=1;i<Token.size()-1;i++)
		{
			printf("%s\n",Token[i].data());
		}
		printf("--------------------------------------\n");
	}
	while(strcmp(szLine,"exit") != 0);

	Shutdown(sock);
}

std::vector<std::string> Tokenize(const std::string & str, const std::string & delim)
{
	using namespace std;
	vector<string> tokens;
	size_t p0 = 0, p1 = string::npos;
	while(p0 != string::npos)
	{
		p1 = str.find_first_of(delim, p0);
		if(p1 != p0)
		{
			string token = str.substr(p0, p1 - p0);
			tokens.push_back(token);
		}
		p0 = str.find_first_not_of(delim, p1);
	}
	return tokens;
}
Không rõ nó, Google Translate demo, sai từ lúc nào?
Error 403 (Forbidden)

Chạy thử từ bản dịch sẵn của RocK


...
..
.