CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    5

    my CHttpFile::SendRequest() timeout when sending request to google translator

    I want to use google translator api through an application I wrote the following code but CHttpFile::SendRequest() method always time out I could n't found what's my problem. Can any one help me?

    CString szTranReq(L"/translate_a/t?client=t&sl=en&tl=tr");

    CString szText(L"text=Hello");

    CString szServer(L"www.google.com");

    DWORD inetPort = 80;




    CInternetSession inetSession(::AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, NULL);

    inetSession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 3);
    inetSession.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
    inetSession.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);

    CHttpConnection* pInetHttpConnection = inetSession.GetHttpConnection(szServer, inetPort, NULL, NULL);


    CHttpFile* pHttpFile = pInetHttpConnection->OpenRequest(
    CHttpConnection::HTTP_VERB_POST,
    szTranReq, NULL, 1, NULL, NULL,
    INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_DONT_CACHE);



    pHttpFile->AddRequestHeaders(L"Host: www.google.com\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"User-Agent: Mozilla/5.0\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"Accept-Encoding: deflate\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"content-length: 5\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"Connection: Close\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);

    pHttpFile->SendRequest(); //always timeout

    Any one could help me with this problem?
    a program with the same coding in QT Works fine

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: my CHttpFile::SendRequest() timeout when sending request to google translator

    You are trying to translate the text "Hello", but I don't see where you actually pass it to the server.
    You just declare the szText variable, but never use it.

  3. #3
    Join Date
    May 2010
    Posts
    5

    Question Re: my CHttpFile::SendRequest() timeout when sending request to google translator

    thanks You were right.
    But unfortunately the following code failed too with Http Status 400(Bad Request)

    CString szTranReq(L"/translate_a/t?client=t&sl=en&tl=tr");

    CString szText(L"text=Hello");

    CString szServer(L"www.google.com");

    DWORD inetPort = 80;




    CInternetSession inetSession(::AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, NULL);

    inetSession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 3);
    inetSession.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
    inetSession.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);

    CHttpConnection* pInetHttpConnection = inetSession.GetHttpConnection(szServer, inetPort, NULL, NULL);


    CHttpFile* pHttpFile = pInetHttpConnection->OpenRequest(
    CHttpConnection::HTTP_VERB_POST,
    szTranReq, NULL, 1, NULL, NULL,
    INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_DONT_CACHE);



    pHttpFile->AddRequestHeaders(L"Host: www.google.com\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"User-Agent: Mozilla/5.0\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"Accept-Encoding: deflate\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"content-length: 10\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
    pHttpFile->AddRequestHeaders(L"Connection: Close\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);

    pHttpFile->SendRequestEx(szText.GetLength(),HSR_INITIATE,1);
    pHttpFile->WriteString(szText);
    pHttpFile->EndRequest();


    DWORD dwRet;
    pHttpFile->QueryInfoStatusCode(dwRet); ///dwRet is always 400

    Actually I Want to implement the following code wriiten in Qt to MFC:


    QString url = QString("/translate_a/t?client=t&sl=en&tl=tr");


    QHttpRequestHeader header = QHttpRequestHeader("POST", url, 1, 1);
    header.setValue("Host", "www.google.com");
    header.setValue("User-Agent", "Mozilla/5.0");
    header.setValue("Accept-Encoding", "deflate");
    header.setContentLength( text.length() );
    header.setValue("Connection", "Close");

    QByteArray ba("text=");
    ba.append( text );

    a_Http->setHost("www.google.com");
    a_Http->request(header,ba);

    a_done.exec(QEventLoop::AllEvents|QEventLoop::WaitForMoreEvents);

    QString out = QString::fromUtf8( a_Http->readAll() );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured