Solved

binding CHttpConnection to a specific IP Address.

Posted on 2004-04-12
11
889 Views
Last Modified: 2013-11-20
Does anybody know of a way to bind a CHttpConnection object to a particular
CLIENT address.  

The winsock equivliant of what I am trying to do would look something like this:
   CSocket client
   client.Create( 0, SOCK_STREAM, "192.168.0.250" );
   client.Connect( "192.168.0.150", 80 );
   client.Write ( <HTTP POST Request> );
   client.Read ( <HTTP POST Response> );
   client.Close();

I would much rather use WinInet (CHttpFile and CHttpConnection) rather than
writing my own HTTP client using winsock.

Thanks in advance
0
Comment
Question by:cathrone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10806635
here's a example from the site http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Internet_First_Steps.3a_.WinInet.asp ..............

get and post are pretty easy with WinInet.

#include <afxinet.h>
//assumes server, port, and URL names have been initialized
CInternetSession session("My Session");
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
try
{
   CString strServerName;
   INTERNET_PORT nPort;

   pServer = session.GetHttpConnection(strServerName, nPort);
   pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
   pFile->AddRequestHeaders(szHeaders);
   pFile->SendRequest();
   pFile->QueryInfoStatusCode(dwRet);

   if (dwRet == HTTP_STATUS_OK)
   {
       UINT nRead = pFile->Read(szBuff, 1023);
       while (nRead > 0)
       {
           //read file...
       }
   }
   delete pFile;
   delete pServer;
}
catch (CInternetException* pEx)
{
   //catch errors from WinInet
}
session.Close();


0
 
LVL 1

Author Comment

by:cathrone
ID: 10807421
This is not what I am looking for, I know how to do posts/gets with CHttpConnection.
I need to bind the CLIENT half of the connection to a particular address.  

To put it another way, the client machine ( the one using CHttpConnection
has more than one IP address.  I need the ability to pick which of those addresses
the request originates from.
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10810643
i dont think there is any way you can bind a ip with CHttpConnection
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:KurtVon
ID: 10814370
I'm a little lost.  I've often done this just be specifying the IP address, as a string, for the server.

    strServerName = "216.71.75.80";
    nPort = 80;
    CHttpConnection* connection = session.GetHttpConnection(strServer, nPort);

seems to work fine for me.
0
 
LVL 1

Author Comment

by:cathrone
ID: 10814638
I am afraid that I am not making myself clear.  My machine ( the client half of the HTTP connection) has multiple IP addresses.  I need the ability to specify which of those IP addresses the request will originate from, the value you specify in GetHttpConnection is the destination IP address.

Here is some more information on the problem.

The HTTP server is running on a machine with an IP address of 192.168.0.150 and a netmask of 255.255.255.0 (changing the IP address or the netmask is NOT an option).  The client machine has an IP address of 192.168.6.239 and a netmask of 255.255.0.0 (making a permanent change of that IP address is also NOT an option).  With this configuration, an HTTP request can get to the server but the response can not get back ( the server's netmask is too restrictive ).

To get around this, I add an IP alias (using the SDK function AddIpAddress) to the client machine.  The alias is 192.168.0.151.  With this, I can bind a CSocket to the alias IP address, connect to the server, and get a response back.   However, when I use GetHttpConnection, the request always originates from the permanent IP address ( used Ehteral to verify), and the respone never gets back.  
 
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 10815433
No, unfortunately the HINTERNET handle does not let you alter the socket properties (to "protect" the user).  Since CInternetSession uses an HINTERNET at its core, there isn't any way to change it.

You can read the socket, though, using InternetQuerySettings on the CInternetSession (it has an overloaded HINTERNET operator) by asking for INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO.  If you know any way to alter a socket on the fly, that would be where to look.

Hope this helps.
0
 
LVL 1

Author Comment

by:cathrone
ID: 10824423
Based on the above tip, I thought the following might work:

Derive a class from CInternetSession and override the OnStatusCallback method.  When
the INTERNET_STATUS_CONNECTING_TO_SERVER event arrived, I queried the
INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO option.  This gave me the unconnected
SOCKET.  I did a winsock bind on the unconnected socket ( the bind did succeed) and
exited the callback.  Unfortuneately, when I do this, the function CHttpFile::SendRequest
throws an exception with error code 12029 (can't connect).  

Just as a test, I did a connect immediately following the bind, and the connect succeeded.
So it appears that HttpSendRequest knows that its socket has been tampered with
and refuses to do the connection.
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 10824655
Or, on exiting the callback, CInternetSession messes with the socket thinking it is still un-bound and causes a failure of some sort.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12049854
PAQed, with points refunded (450)

Computer101
E-E Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sum28 challenge 31 117
matchUp  challenge 6 82
wait notify demo infinite loop 3 148
Change to event 1 135
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question