GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Jan-2007, 12:20
HarmoniousBotch HarmoniousBotch is offline
New Member
 
Join Date: Jan 2007
Posts: 5
HarmoniousBotch is on a distinguished road

HTTP post program generates 302 moved error


I'm working on a simple program to post to a web site and display the response on the screen. The problem is that I keep getting a "302 moved temporarily" error. Whatever file I ask for, I get the same error.
A few lines below the error message is a line that says "Location:" and then the URL that I am trying to post to. That URL is the same as the one I used in the post command. Yet if I open a browser and stick that URL in it, the page comes up ok - this really confuses me.

The code is as follows:
CPP / C++ / C Code:
#define LF 10
#define CR 13
#define NUL 0

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

char *strcat(char *s1, const char *s2);
int  connect(int socket, const struct sockaddr *address, socklen_t address_len);
void bzero(char *s, int n);
char *strcpy (const char *dest,const char *src);
int  strlen(const char *string);
int  read(int sockfd, char *buf, unsigned int nbytes);
int  write(int sockfd, char *buf, unsigned int nbytes);
void bcopy(const void *s1, void *s2, size_t n);
void exit(int status);

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n, i;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    printf( "444444444444444444444444444444444444444444444444444444444444444444444444\n" );


    char crlfstr[3];
    crlfstr[0] = CR; 
    crlfstr[1] = LF;
    crlfstr[2] = NUL;

    char buffer[256];
    
    portno = 80;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname("abebooks.com");
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect( sockfd, &serv_addr, sizeof(serv_addr) ) < 0) 
        error("ERROR connecting");

    bzero(buffer,256);

    strcpy( buffer, "POST " );
    strcat( buffer, "/servlet/SearchResults?isbn=0380718774" );
    strcat( buffer, " HTTP/1.1" );
    strcat( buffer, crlfstr );

    strcat( buffer, "Content-Type:application/x-www-form-urlencoded" );
    strcat( buffer, crlfstr );

    strcat( buffer, "Host: www.abebooks.com" );

    strcat( buffer, crlfstr );

    strcat( buffer, crlfstr ); //blank line

    strcat( buffer, crlfstr );  //terminal blank line

    printf("%s\n", buffer );


    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) {
      error("ERROR writing to socket");
    }
    else printf( "Wrote to socket without errors\n"); 

    for ( i =1; i<=50; i++){
      bzero(buffer,256);
      n = read(sockfd,buffer,255);
      if (n < 0) {
         error("ERROR - reading from socket");
      } else printf("%s\n",buffer);
    }
    
    printf( "===================================================================\n" );
    return 0;
}


It looks like this when I run it:
Quote:

44444444444444444444444444444444444444444444444444 444444444444444444444
POST /servlet/SearchResults?isbn=0380718774 HTTP/1.1
Content-Type:application/x-www-form-urlencoded
Host: http://www.abebooks.com


Wrote to socket without errors
HTTP/1.1 302 Moved Temporarily
Date: Sun, 07 Jan 2007 23:48:15 GMT
Location: http://www.abebooks.com/servlet/SearchResults?isbn=0380718774
Content-Type: text/plain; charset=ISO-8859-1
Content-Length: 0
Vary: Accept-Encoding,User-Agent
Edge-control:
no-store
Cashe-control: private
(* then about 25-30 line feeds *)
================================================== ===================

I'd really appreciate it if someone could tell me how to interpret the error message, and maybe even how to fix the program.
BTW, this is my first post to this forum, so apologies to all if my etiquette is not ok.
Thanks. -HB
  #2  
Old 14-Jan-2007, 12:24
HarmoniousBotch HarmoniousBotch is offline
New Member
 
Join Date: Jan 2007
Posts: 5
HarmoniousBotch is on a distinguished road

Re: HTTP post program generates 302 moved error


The crucial line in my post above is not showing up correctly.

It should be:
Quote:
Location: http://www.abebooks.com/servlet/SearchResults?isbn=0380718774
  #3  
Old 14-Jan-2007, 12:28
HarmoniousBotch HarmoniousBotch is offline
New Member
 
Join Date: Jan 2007
Posts: 5
HarmoniousBotch is on a distinguished road

Third try


The line still does not appear as it should. It keeps getting truncated. One more try:

After "Location: www.abebooks.com" the following should be on the same line:

/servlet/SearchResults?isbn=0380718774
  #4  
Old 14-Jan-2007, 15:08
HarmoniousBotch HarmoniousBotch is offline
New Member
 
Join Date: Jan 2007
Posts: 5
HarmoniousBotch is on a distinguished road

Re: HTTP post program generates 302 moved error


Thanks for replying.

Yes, I tried 'Get'. It worked fine. This is a modified version of that program.

I don't understand what you mean by 'set up for'. Is my request formatted improperly?
  #5  
Old 14-Jan-2007, 16:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,701
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: HTTP post program generates 302 moved error


Quote:
Originally Posted by HarmoniousBotch
Thanks for replying.

Yes, I tried 'Get'. It worked fine. This is a modified version of that program.

I don't understand what you mean by 'set up for'. Is my request formatted improperly?

I'm sorry that I posted too quickly and deleted it (but, obviously not quickly enough).

The sequence that you posted works with some web servers but not others. I share your confusion, since the server is supposed to send the redirected address, but obviously didn't in this case. (Since sending the "POST" request to that URI gave this message in the first place.)

Using "GET" instead of "POST" works to retrieve the searched-for information (and "GET" is appropriate for read-only, fixed information), but there's no reason that they shouldn't have implemented "POST" properly (or not at all).

Sorry about the confusion with my post and even more sorry that I don't have any insight into anything you could do differently for this web site. Maybe someone else has some ideas and/or workarounds.

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
error template with C linkage MichaelS-R C++ Forum 6 17-May-2006 11:09
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 10:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
What is "Ambigious symbol" ??*( a compilation error) small_ticket C++ Forum 2 07-Jan-2005 21:10
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 16:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.