I have a C program using winsock that connect to internet.
however, now I am in Http Proxy environment that need to pass through the proxy before I can connect to internet.
my program now is not support this. so Http/1.0 bad request is results.
How I can support the Http proxy, this proxy need a username and password.
I also attached my function to connect the internet, very simple single-thread connection
int GetInternetBuffer(char *servername, char *HTTPcommand){
int istrBufferindex=0;
char *buff2;
buff2 = new char [10240];
if (buff2 == NULL){
printf("buff2 Memory Error!!\n");
return -1;
}
WSADATA wsaData;
struct hostent *hp;
unsigned int addr;
struct sockaddr_in server;
WORD version = MAKEWORD( 2, 0 );
int wsaret=WSAStartup(version,&wsaData);
if(wsaret){
printf("WSAStartup failed!\n");
return -1;
}
SOCKET conn;
conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(conn==INVALID_SOCKET){
printf("socket create failed!\n!");
return -1;
}
if(inet_addr(servername)==INADDR_NONE)
{
hp=gethostbyname(servername);
}
else
{
addr=inet_addr(servername);
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}
if(hp==NULL)
{
closesocket(conn);
printf("Can't get host by address on %s!!\n", servername);
return -1;
}
server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
server.sin_family=AF_INET;
server.sin_port=htons(80);
int iFailConnectCount=0; sprintf(buff2,"GET %s\r\n\r\n", HTTPcommand);
send(conn,buff2,strlen(buff2),0);
int y;
for (int ibuffindex=0; ibuffindex<10240; ibuffindex++){
buff2[ibuffindex]='\0';
}
// put the downloaded data to strBuffer
istrBufferindex = 0;
ibuffindex=0;
struct timeval new_tv;
setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (char*)&new_tv, sizeof(new_tv));
while(y=recv(conn,buff2,10240,0)>0)
{
for (ibuffindex=0;ibuffindex<10240;ibuffindex++,istrBufferindex++){
:
:
}
}
// clear the contents of buff
for (int ibuffindex=0; ibuffindex<10240; ibuffindex++){
buff2[ibuffindex]='\0';
}
}
delete [] buff2;
closesocket(conn);
WSACleanup();
}