-
HTTP to HTTPS
Hi there,
I was wondering if anyone could help me with a problem have - i currently post to a site and then read back all the info i get using sockets
the prob is i need to modify this code to accomodate https servers?
any ideas
code posted below:
Any ideas as to what i should change to modify it???
WSADATA wsa;
SOCKADDR_IN LocalAddr, RemoteAddr;
SOCKET sock;
fd_set l_set;
struct timeval l_tv;
char cbuf[1], tmpbuf[10], buffer[2500], allbuf[2500];
char *retchar = 0;
int empfangene_bytes;
int l_rc;
char tmp[50];
char l_debug = 1;
if (l_debug) printf("\nIn vts_send_XML_thread\n");
WSAStartup(MAKEWORD(1,1), &wsa);
strnset(pSendXML->XML_Recv_String,0,sizeof(pSendXML->XML_Recv_String));
// Create Socket //
//***************//
memset(&RemoteAddr, 0, sizeof(SOCKADDR_IN));
RemoteAddr.sin_family = AF_INET;
RemoteAddr.sin_port = htons(80);
RemoteAddr.sin_addr.s_addr = inet_addr(pSendXML->XML_HOST);
if (l_debug) printf("\nCreating a Socket - ");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // returns a descriptor referencing the new socket
if (sock == INVALID_SOCKET)
{
printf("\n\nSocket Create Error(%d)(%d)", sock, WSAGetLastError());
pSendXML->rc = -1;
WSACleanup();
_endthread();
}
if (l_debug) printf(" - Success(%d)", sock);
// Bind //
//******//
if (l_debug) printf("\nTrying to Bind");
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = htonl (INADDR_ANY);
LocalAddr.sin_port = htons(0);
l_rc = bind (sock, (struct sockaddr *) &LocalAddr, sizeof (LocalAddr));
if (l_rc == SOCKET_ERROR)
{
printf("\n\nSocket Bind Error(%d)(%d)", l_rc, WSAGetLastError());
pSendXML->rc = -2;
WSACleanup();
_endthread();
}
if (l_debug) printf(" - Success(%d)",l_rc);
// Connect //
//*********//
if (l_debug) printf("\nTrying to Connect");
l_rc = connect(sock, (SOCKADDR*) &RemoteAddr, sizeof(SOCKADDR_IN)); // If no error occurs, connect returns zero
if (l_rc == SOCKET_ERROR)
{
printf("\n\nSocket Connect Error(%d)(%d)", l_rc, WSAGetLastError());
pSendXML->rc = -3;
WSACleanup();
_endthread();
}
if (l_debug) printf(" - Success(%d)",l_rc);
// Send Data //
//***********//
strnset(buffer,0,sizeof(buffer));
strcpy(buffer,"");
sprintf(tmp,"Content-Length: %d\r\n", strlen(pSendXML->XML_Send_String));
strcat(buffer,"GET ");
strcat(buffer,pSendXML->XML_Send_String);
strcat(buffer," HTTP/1.1\r\n");
strcat(buffer,"Host: ");
strcat(buffer,pSendXML->XML_HOST);
strcat(buffer,"\r\n");
strcat(buffer,"\r\n");
if (l_debug) printf("\nBuffer(S) = *%s*",buffer);
l_rc = send(sock, buffer, strlen(buffer), 0); // If no error occurs, send returns the total number of bytes sent
if (l_rc == SOCKET_ERROR)
{
printf("\n\nSocket Send Error(%d)(%d)", l_rc, WSAGetLastError());
pSendXML->rc = -4;
WSACleanup();
_endthread();
}
if (l_debug) printf("\nSent l_rc = %d",l_rc);
// Recieve Data //
//**************//
l_tv.tv_sec = pSendXML->TimeOut_Limit;
l_tv.tv_usec= 0;
FD_ZERO(&l_set);
FD_SET (sock, &l_set);
if (l_debug) printf("\nLets Check for Data");
l_rc = select(FD_SETSIZE, &l_set, NULL, NULL, &l_tv); // do a quick check to see if we got anything waiting
//l_rc = select(FD_SETSIZE, &l_set, NULL, NULL, NULL); // do a quick check to see if we got anything waiting
if ((l_rc == SOCKET_ERROR) || (l_rc == 0))
{
printf("\n\nSocket Select Error(%d)(%d)", l_rc, WSAGetLastError());
pSendXML->rc = -5;
WSACleanup();
_endthread();
}
printf("- Select (%d)", l_rc);
if (l_debug) printf("\nAttempting to Recieve(%d)", sizeof(buffer));
empfangene_bytes = 1;
strnset(allbuf,0,sizeof(allbuf));
while ((empfangene_bytes != 0) && (strstr(allbuf,"?xml") == NULL))
{
empfangene_bytes = recv(sock, cbuf, 1,0);
if ((empfangene_bytes == SOCKET_ERROR) && (WSAGetLastError() != 10038) && (pSendXML->TimeOut_Current < pSendXML->TimeOut_Limit))
{
printf("\n\nSocket(1) Recieve Error(%d)(%d)", empfangene_bytes, WSAGetLastError());
pSendXML->rc = -6;
WSACleanup();
_endthread();
}
if (strlen(allbuf)+1 < sizeof(allbuf))
{
if ( (cbuf[0] != ' ') && (cbuf[0] != '\n') ) sprintf(allbuf,"%s%c", allbuf, cbuf[0]);
}
else
{
//empfangene_bytes = 0;
strnset(allbuf,0,sizeof(allbuf));
}
}
if ( (empfangene_bytes == 0) || (strstr(allbuf,"?xml") == NULL) )
{
strncpy(pSendXML->XML_Recv_String,allbuf,sizeof(pSendXML->XML_Recv_String));
printf("\n\nNo XML feed found(%d)(%d)\n**%s**", empfangene_bytes, WSAGetLastError(), allbuf);
pSendXML->rc = -7;
WSACleanup();
_endthread();
}
strnset(buffer,0,sizeof(buffer));
strcat(buffer,"<?xml");
strcpy(tmpbuf,"");
l_rc = 0;
while ((empfangene_bytes != 0) && (l_rc == 0))
{
empfangene_bytes = recv(sock, cbuf, 1,0);
if ((empfangene_bytes == SOCKET_ERROR) && (WSAGetLastError() != 10038) && (pSendXML->TimeOut_Current < pSendXML->TimeOut_Limit))
{
printf("\n\nSocket(2) Recieve Error(%d)(%d)", empfangene_bytes, WSAGetLastError());
pSendXML->rc = -8;
WSACleanup();
_endthread();
}
// Search and Replace
if ((cbuf[0] == '&') || ((cbuf[0] == ';') && (strlen(tmpbuf) > 0)) || (strlen(tmpbuf) > 0))
{
sprintf(tmpbuf,"%s%c", tmpbuf, cbuf[0]);
//if (l_debug) printf ("\n\t*%s*", tmpbuf);
}
if ((strlen(tmpbuf) > 0) && (cbuf[0] == ';'))
{
if (strcmp(tmpbuf,"<") == 0) {cbuf[0] = '<'; strcpy(tmpbuf,"");}
if (strcmp(tmpbuf,">") == 0) {cbuf[0] = '>'; strcpy(tmpbuf,"");}
}
if ( (strlen(tmpbuf) == 0) )
/*&& ((cbuf[0] != ' ') && (buffer[strlen(buffer)-1] != ' ')) )*/
/*&& ((cbuf[0] != '\n') && (buffer[strlen(buffer)-1] != '\n')) ) */
{
if (strlen(buffer)+1 < sizeof(buffer)) sprintf(buffer,"%s%c", buffer, cbuf[0]);
else l_rc = 1;
}
}
strncpy(pSendXML->XML_Recv_String,buffer,sizeof(pSendXML->XML_Recv_String));
if (l_debug) printf("\nBuffer(R) = *%s*",buffer);
if (l_debug) printf("\n\nWe Are Done\n");
pSendXML->rc = 0;
closesocket(sock);
//WSACleanup();
_endthread();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks