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 (