Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
i am trying using below code for connect to server but there are some errors.
Can anybody help me?
C++
/************************************************************
*                                                           *
* RetOptions.cpp                                            *
*                                                           *
* Copyright (c) Microsoft Corporation. All Rights Reserved. *
*                                                           *
************************************************************/

#include #windows.h#
#include #winhttp.h#;
#include #stdio.h#


int main(int argc, char* argv[])
{
    HINTERNET hSession;
    HINTERNET hConnect;
    HINTERNET hRequest;
    BOOL httpResult;
    DWORD data;
    DWORD dwSize = sizeof(DWORD);
    LPWSTR pwszURL;


    // Print a description of the sample.
    printf("This sample demonstrates the process for retrieving WinHTTP\n");
    printf("options programmatically from a C/C++ application. WinHTTP\n");
    printf("application programming interface (API) functions are used to\n");
    printf("determine the default connect time-out value and to find the\n");
    printf("current URL after an HTTP request is redirected.\n\n");

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", 
                             WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                             WINHTTP_NO_PROXY_NAME, 
                             WINHTTP_NO_PROXY_BYPASS, 0);

    // Use WinHttpQueryOption to retrieve Internet options.
    httpResult = WinHttpQueryOption( hSession, WINHTTP_OPTION_CONNECT_TIMEOUT, 
                                     &data, &dwSize);
    if (httpResult) 
        printf("Connect time-out:\t%u ms\n",data);

    // Use WinHttpConnect to specify an HTTP server.
    hConnect = WinHttpConnect( hSession, L"msdn.microsoft.com",
                               INTERNET_DEFAULT_HTTP_PORT, 0);

    // Open and Send a Request Header.
    hRequest = WinHttpOpenRequest( hConnect, L"GET", 
                         L"/downloads/samples/internet/winhttp/retoptions/redirect.asp", 
                         NULL, WINHTTP_NO_REFERER, 
                         WINHTTP_DEFAULT_ACCEPT_TYPES, 0);                     
    httpResult = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 
                                     0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    httpResult = WinHttpReceiveResponse( hRequest, NULL);

    // Use WinHttpQueryOption again, this time to obtain a buffer size.
    httpResult = WinHttpQueryOption( hRequest, WINHTTP_OPTION_URL, 
                                     NULL, &dwSize);
    pwszURL = new WCHAR[dwSize/sizeof(WCHAR)];
    
    // Use WinHttpQueryOption to retrieve Internet options.
    httpResult = WinHttpQueryOption( hRequest, WINHTTP_OPTION_URL, 
                                     (void *)pwszURL, &dwSize);
    printf("Redirected URL:\t\t%S\n",pwszURL);

    // Free the allocated memory.
    delete [] pwszURL;

    // When finished, release the hRequest handle.
    httpResult = WinHttpCloseHandle(hRequest);
    if (!httpResult)
        printf("Could not close the hRequest handle.\n");

    // When finished, release the hConnect handle.
    httpResult = WinHttpCloseHandle(hConnect);
    if (!httpResult)
        printf("Could not close the hConnect handle.\n");

    // When finished, release the hSession handle.
    httpResult = WinHttpCloseHandle(hSession);
    if (!httpResult)
        printf("Could not close the hSession handle.\n");


    return 0;
}


my error is as below:

1>------ Build started: Project: 111, Configuration: Debug Win32 ------
1>Compiling...
1>111.cpp
1>Linking...
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpCrackUrl@16 referenced in function _wmain
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpCloseHandle@4 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpQueryHeaders@24 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpReceiveResponse@8 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpWriteData@16 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpSendRequest@28 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpAddRequestHeaders@16 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpenRequest@28 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpConnect@16 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>111.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpen@20 referenced in function "int __cdecl WinHttpFormSubmitSample(wchar_t const *,unsigned short,wchar_t *,int,wchar_t *)" (?WinHttpFormSubmitSample@@YAHPB_WGPA_WH1@Z)
1>C:\Users\Reza\Desktop\111\Debug\111.exe : fatal error LNK1120: 10 unresolved externals
1>Build log was saved at "file://c:\Users\Reza\Desktop\111\111\Debug\BuildLog.htm"
1>111 - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Best Regards,
Posted
Updated 6-May-10 9:42am
v3

I think perhaps you forgot to add Winhttp.lib to your linker options.
 
Share this answer
 
Dear Sir,

Thank you for your reply.
Yes, you are right. I added that and it works now.

I am a beginner in C++.
Now I can connect to my website and want to know how I must add my parameter in the above code for sending request to server and after that, how to get response from server for using that in my program.

Thanks!
 
Share this answer
 
v2
Comments
Richard MacCutchan 7-May-10 6:33am    
I'm afraid I have not used these services so cannot help, but you should be able to find what you need in MSDN. Try this link http://msdn.microsoft.com/en-us/library/aa384273(VS.85).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900