C + + program implementation accesses Web page content through HTTP proxy __c++

Source: Internet
Author: User
Tags sin

The company through the proxy Internet, c program directly through the HTTP request can not get the Web page content, it has implemented a proxy access to the HTTP Web page of a test program.

The program is simple, with several points

Direct connection to proxy server via socket first

Send the HTTP connect header to the proxy server in the form of connect www.baidu.com:80 http/1.0\r\nproxy-authorization:basic%s\r\n\r\n

Where%s is replaced with the base64 encoding of USER:PASSWD

After authentication is passed, send request OK, note that the difference between this and no agent is that the address after get is http://www.baidu.com and not www.baidu.com

This is a test program, the purpose is only to explain the implementation process, I test can receive the return message, there are imperfections, such as receiving incomplete.


The code is as follows:[CPP]  View plain  copy   #include  <sys/socket.h>   #include  <netinet/in.h >   #include  <arpa/inet.h>   #include  <netdb.h>   #include  <unistd.h>   #include  <stdio.h>   #include  <errno.h>    #include  <string>     //The following six parameters to change their own needs can    const char * Proxyaddr= "10.1.1.2";   const int proxyport = 8080;   Const char  *user= "Domain\\name";   const char *passwd= "Kklklkl";   Const char  *desthost= "www.baidu.com";   const int destport=80;   static void  To64frombits (Unsigned char *out, const unsigned char *in, int inlen);       int main ()  {       int sock_fd; &nbsP     struct sockaddr_in addr;       struct  hostent *hptr;       char str[32];               /*  if ((Hptr = gethostbyname (proxyaddr))  ==  NULL)           {               printf (" gethostbyname error for host:%s\n",  proxyAddr) ;              return 0;           }      */            if  ((Sock_fd = socket (af_inet, sock_stream, 0))  < 0)            perror ("socket ()  fail.");               memset (&addr, 0, sizeof (addr));        addr.sin_family = AF_INET;       addr.sin _port = htons (ProxyPort);       inet_aton (proxyAddr, &addr.sin _ADDR);       //memcpy (&addr.sin_addr.s_addr,hptr->h_addr,sizeof (addr.sin_ ADDR.S_ADDR));       //printf ("proxy ip=%s\n", Inet_ntop (Hptr->h_addrtype, Hptr->h_addr,str,sizeof (str));       if  Connect (sock_fd,  (struct  sockaddr *) &addr, sizeof (addr)  < 0)  {            close (SOCK_FD);            Perror ("Connect ()  fail.");        }       printf (

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.