How to open http_listener (error -> Address 'http://www.somesite.com/' is already in use)

Sep 11, 2014 at 12:10 PM
Hello

i always getting error that address is in use when using this code
void ListenerOpen()
{
    try {
        web::http::experimental::listener::http_listener listener(L"http://www.google.com/");
        listener.support(([&](web::http::http_request request){ request.reply(web::http::status_codes::OK, U("Hello, World!"));  }));
        listener.open().wait();
        }
    catch (std::exception &e) {
        std::cout << "caught error: " << e.what() << std::endl; cin.get();
    }
}
error appear when i try to open listener.

Do you have some advice, or some good link to know why is this happen?
Coordinator
Sep 11, 2014 at 5:33 PM
Hi silent_,

Are you running the code in windows or Linux? What version of CppRest SDK are you using?

Thanks
Sep 11, 2014 at 8:30 PM
hi zhelu,

version is C++ REST SDK 1.4.0, i run on windows 7 64x, visual studio 2013
Coordinator
Sep 11, 2014 at 8:32 PM
Hi silent_,

Without actually running anything my guess is that either some other service is actually already listening on port 80 or you aren't running your program with administrative privileges on Windows. On Windows to listen on port 80 this is a requirement.

Steve
Sep 11, 2014 at 8:44 PM
hi stevegates,

none of service running on port 80, and i have all privileges. I just download your examples from here, latest version "https://casablanca.codeplex.com/SourceControl/latest" and compile project bingrequest -> which work, and in same project put above code and try to run but always getting error that address is in use (when i put instead of google.com http://localhost:8080/hello it's working).
Coordinator
Sep 11, 2014 at 10:24 PM
Hi silent_,

I think you're getting confused between the functionality of the listener and the client.

If you want to fetch a webpage, like in the BingRequest sample, you need to use the client. With the client, you can use any URL (such as http://www.bing.com/ and others).

However, if you want others to fetch webpages from you, you need to use the listener. With the listener, you can only use URLs that point to you. This usually means you need to use "localhost" instead of "www.example.com". This is because "www.example.com" doesn't point to you, it points to a site run by IANA.

Sincerely,
roschuma
Sep 11, 2014 at 11:11 PM
hi roschuma,

you have definitely right, i'm new in rsdk so many thing seems confused to me. I just realize that i need something like interceptor. My goal is to display in browser that site status is Forbidden when i get to the google.com. (like in this example http://blogs.msdn.com/b/vcblog/archive/2013/07/10/intercepting-http-request_2f00_response-using-c_2b002b00_-rest-http-library.aspx) ->butt i can get it work.

This is what i already try to intercept google and display in browser that site is Forbidden
void IntercetGoogle()
{
auto testAuthStage =
    [](http_request request, std::shared_ptr<http_pipeline_stage> next_stage) -> pplx::task < http_response >
{
    http_response response;
    response.set_status_code(status_codes::Forbidden);
    wcout << response.extract_string().get() << endl;
    return pplx::task_from_result(response);
};

http_client client(L"http://google.com");
client.add_handler(testAuthStage);
}
``` This code don't work it want display in browser that site is Forbidden, why?