Post-problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerard van Wilgen

    Post-problem

    I have a client-application that uses a socket to post some data to a
    server-application. The following piece of code in the client takes care of
    the posting:

    $sock = fsockopen("www. majstro.com", 80, $errno, $errstr, 30);
    if ($sock)
    {
    $data = 'a=0&b=1';
    fputs($sock, "POST /Web/Majstro/SD_Server.php HTTP/1.0\r\n");
    fputs($sock, "Host: www.majstro.com \r\n");
    fputs($sock, "Accept: */*\r\n");
    fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
    fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n") ;
    fputs($sock, "$data\r\n" );
    $headers = "";
    while ($str = trim(fgets($soc k, 4096)))
    $headers .= "$str\n";
    $body = "";
    while (!feof($sock))
    $body .= fgets($sock, 4096);
    fclose($sock);
    return $body;
    }

    My problem is that SD_Server.php is indeed called, but that it is unable to
    extract the parameters from the HTTP-header. They are not present in $_POST,
    and count($HTTP_POS T_VARS) returns the value 0. On the other hand,
    $_SERVER['CONTENT_LENGTH '] contains the value 7, which is exactly the string
    length of "a=0&b=1". So, it seems that the data is present in the header,
    but for some reason PHP just cannot parse it.

    I have run out of ideas to make this work. Has anybody here a suggestion? It
    might work if I use the get method instead of the post method, but I very
    much like to know what is wrong with this code.


    Gerard van Wilgen
    --
    www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
    www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
    tradukvortaro por Windows)

  • CountScubula

    #2
    Re: Post-problem

    > fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n") ;

    too many \r\n\r\n



    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools
    "Gerard van Wilgen" <gvanwilgen@pla net.nl> wrote in message
    news:bve9p5$j2n $1@reader10.wxs .nl...[color=blue]
    > I have a client-application that uses a socket to post some data to a
    > server-application. The following piece of code in the client takes care[/color]
    of[color=blue]
    > the posting:
    >
    > $sock = fsockopen("www. majstro.com", 80, $errno, $errstr, 30);
    > if ($sock)
    > {
    > $data = 'a=0&b=1';
    > fputs($sock, "POST /Web/Majstro/SD_Server.php HTTP/1.0\r\n");
    > fputs($sock, "Host: www.majstro.com \r\n");
    > fputs($sock, "Accept: */*\r\n");
    > fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
    > fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n") ;
    > fputs($sock, "$data\r\n" );
    > $headers = "";
    > while ($str = trim(fgets($soc k, 4096)))
    > $headers .= "$str\n";
    > $body = "";
    > while (!feof($sock))
    > $body .= fgets($sock, 4096);
    > fclose($sock);
    > return $body;
    > }
    >
    > My problem is that SD_Server.php is indeed called, but that it is unable[/color]
    to[color=blue]
    > extract the parameters from the HTTP-header. They are not present in[/color]
    $_POST,[color=blue]
    > and count($HTTP_POS T_VARS) returns the value 0. On the other hand,
    > $_SERVER['CONTENT_LENGTH '] contains the value 7, which is exactly the[/color]
    string[color=blue]
    > length of "a=0&b=1". So, it seems that the data is present in the header,
    > but for some reason PHP just cannot parse it.
    >
    > I have run out of ideas to make this work. Has anybody here a suggestion?[/color]
    It[color=blue]
    > might work if I use the get method instead of the post method, but I very
    > much like to know what is wrong with this code.
    >
    >
    > Gerard van Wilgen
    > --
    > www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
    > www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
    > tradukvortaro por Windows)
    >[/color]


    Comment

    • Gerard van Wilgen

      #3
      Re: Post-problem


      "CountScubu la" <me@scantek.hot mail.com> wrote in message
      news:UUzSb.7958 $827.3100@newss vr29.news.prodi gy.com...[color=blue][color=green]
      > > fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n") ;[/color]
      >
      > too many \r\n\r\n[/color]

      I do not think so. Every example that I have seen had an empty line between
      the headers and the data, as is required by the HTTP-specs. Anyway, I have
      tried it without an empty line, just to be sure. and that only resulted in
      an "invalid header" message.

      Gerard van Wilgen
      --
      www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
      www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
      tradukvortaro por Windows)




      Comment

      • CountScubula

        #4
        Re: Post-problem

        "Gerard van Wilgen" <gvanwilgen@pla net.nl> wrote in message
        news:bvemrs$e33 $1@reader08.wxs .nl...[color=blue]
        >
        > "CountScubu la" <me@scantek.hot mail.com> wrote in message
        > news:UUzSb.7958 $827.3100@newss vr29.news.prodi gy.com...[color=green][color=darkred]
        > > > fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n") ;[/color]
        > >
        > > too many \r\n\r\n[/color]
        >
        > I do not think so. Every example that I have seen had an empty line[/color]
        between[color=blue]
        > the headers and the data, as is required by the HTTP-specs. Anyway, I have
        > tried it without an empty line, just to be sure. and that only resulted in
        > an "invalid header" message.
        >
        > Gerard van Wilgen
        > --
        > www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
        > www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
        > tradukvortaro por Windows)
        >
        >
        >
        >[/color]

        Ok, no problem, I do not know if your posting vars or data, to do vars,
        there is no blank line, to post data, yes there is.

        data: content-length + blank line + data
        vars: no content-length + no blank line + vars + then blanks lines

        read a little closer, and grab a packet sniffer and watch the packets before
        you try and create a packet without even knowing what it looks like.

        here is a super simple one:


        --
        Mike Bradley
        http://www.gzentools.com -- free online php tools


        Comment

        • Chung Leong

          #5
          Re: Post-problem

          You got an extra dash in the content type. It's
          "applicatio n/x-www-form-urlencoded"

          :-)

          Uzytkownik "Gerard van Wilgen" <gvanwilgen@pla net.nl> napisal w wiadomosci
          news:bve9p5$j2n $1@reader10.wxs .nl...[color=blue]
          > I have a client-application that uses a socket to post some data to a
          > server-application. The following piece of code in the client takes care[/color]
          of[color=blue]
          > the posting:
          >
          > $sock = fsockopen("www. majstro.com", 80, $errno, $errstr, 30);
          > if ($sock)
          > {
          > $data = 'a=0&b=1';
          > fputs($sock, "POST /Web/Majstro/SD_Server.php HTTP/1.0\r\n");
          > fputs($sock, "Host: www.majstro.com \r\n");
          > fputs($sock, "Accept: */*\r\n");
          > fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
          > fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n") ;
          > fputs($sock, "$data\r\n" );
          > $headers = "";
          > while ($str = trim(fgets($soc k, 4096)))
          > $headers .= "$str\n";
          > $body = "";
          > while (!feof($sock))
          > $body .= fgets($sock, 4096);
          > fclose($sock);
          > return $body;
          > }
          >
          > My problem is that SD_Server.php is indeed called, but that it is unable[/color]
          to[color=blue]
          > extract the parameters from the HTTP-header. They are not present in[/color]
          $_POST,[color=blue]
          > and count($HTTP_POS T_VARS) returns the value 0. On the other hand,
          > $_SERVER['CONTENT_LENGTH '] contains the value 7, which is exactly the[/color]
          string[color=blue]
          > length of "a=0&b=1". So, it seems that the data is present in the header,
          > but for some reason PHP just cannot parse it.
          >
          > I have run out of ideas to make this work. Has anybody here a suggestion?[/color]
          It[color=blue]
          > might work if I use the get method instead of the post method, but I very
          > much like to know what is wrong with this code.
          >
          >
          > Gerard van Wilgen
          > --
          > www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
          > www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
          > tradukvortaro por Windows)
          >[/color]


          Comment

          • Rahul Anand

            #6
            Re: Post-problem

            "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<0PCdneOys bZjeIfdRVn-vg@comcast.com> ...[color=blue]
            > You got an extra dash in the content type. It's
            > "applicatio n/x-www-form-urlencoded"
            >
            > :-)
            >[/color]

            Right, I think modifying content-type will solve your problem.

            Comment

            • Gerard van Wilgen

              #7
              Re: Post-problem


              "Rahul Anand" <rahulanand_bis @rediffmail.com > wrote in message
              news:628e2f7b.0 401310131.1a165 99b@posting.goo gle.com...[color=blue]
              > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message[/color]
              news:<0PCdneOys bZjeIfdRVn-vg@comcast.com> ...[color=blue][color=green]
              > > You got an extra dash in the content type. It's
              > > "applicatio n/x-www-form-urlencoded"
              > >
              > > :-)
              > >[/color]
              >
              > Right, I think modifying content-type will solve your problem.[/color]

              The content-type was indeed incorrect. Unfortunately, changing it did not
              change anything.

              Gerard van Wilgen
              --
              www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
              www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
              tradukvortaro por Windows)

              Comment

              • CountScubula

                #8
                Re: Post-problem

                "Gerard van Wilgen" <gvanwilgen@pla net.nl> wrote in message
                news:bvg4pk$sc$ 1@reader11.wxs. nl...[color=blue]
                >
                > "Rahul Anand" <rahulanand_bis @rediffmail.com > wrote in message
                > news:628e2f7b.0 401310131.1a165 99b@posting.goo gle.com...[color=green]
                > > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message[/color]
                > news:<0PCdneOys bZjeIfdRVn-vg@comcast.com> ...[color=green][color=darkred]
                > > > You got an extra dash in the content type. It's
                > > > "applicatio n/x-www-form-urlencoded"
                > > >
                > > > :-)
                > > >[/color]
                > >
                > > Right, I think modifying content-type will solve your problem.[/color]
                >
                > The content-type was indeed incorrect. Unfortunately, changing it did not
                > change anything.
                >
                > Gerard van Wilgen
                > --
                > www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
                > www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
                > tradukvortaro por Windows)
                >[/color]


                here is a capture from a packet sniffer, notice no blank line:

                POST http://www-1.gzentools.com/browserchk.php HTTP/1.1
                Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
                application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword,
                application/x-shockwave-flash, */*
                Referer: http://www-1.gzentools.com/browserchk.php
                Accept-Language: en-us
                Content-Type: application/x-www-form-urlencoded
                Connection: Close
                User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
                Host: www-1.gzentools.com
                Content-Length: 67
                Pragma: no-cache
                Cookie: sm=browser-server
                field_textbox=t est+post+data&f ield_hidden=Thi s+is+hidden+fie ld+data



                --
                Mike Bradley
                http://www.gzentools.com -- free online php tools


                Comment

                • Gerard van Wilgen

                  #9
                  Re: Post-problem


                  "CountScubu la" <me@scantek.hot mail.com> wrote in message
                  news:2FMSb.1828 9$wG1.11561@new ssvr27.news.pro digy.com...[color=blue]
                  > "Gerard van Wilgen" <gvanwilgen@pla net.nl> wrote in message
                  > news:bvg4pk$sc$ 1@reader11.wxs. nl...[color=green]
                  > >
                  > > "Rahul Anand" <rahulanand_bis @rediffmail.com > wrote in message
                  > > news:628e2f7b.0 401310131.1a165 99b@posting.goo gle.com...[color=darkred]
                  > > > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message[/color]
                  > > news:<0PCdneOys bZjeIfdRVn-vg@comcast.com> ...[color=darkred]
                  > > > > You got an extra dash in the content type. It's
                  > > > > "applicatio n/x-www-form-urlencoded"
                  > > > >
                  > > > > :-)
                  > > > >
                  > > >
                  > > > Right, I think modifying content-type will solve your problem.[/color]
                  > >
                  > > The content-type was indeed incorrect. Unfortunately, changing it did[/color][/color]
                  not[color=blue][color=green]
                  > > change anything.
                  > >
                  > > Gerard van Wilgen
                  > > --
                  > > www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
                  > > www.travlang.com/Ergane (Free translation dictionary for Windows /[/color][/color]
                  Senpaga[color=blue][color=green]
                  > > tradukvortaro por Windows)
                  > >[/color]
                  >
                  >
                  > here is a capture from a packet sniffer, notice no blank line:
                  >
                  > POST http://www-1.gzentools.com/browserchk.php HTTP/1.1
                  > Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
                  > application/vnd.ms-powerpoint, application/vnd.ms-excel,[/color]
                  application/msword,[color=blue]
                  > application/x-shockwave-flash, */*
                  > Referer: http://www-1.gzentools.com/browserchk.php
                  > Accept-Language: en-us
                  > Content-Type: application/x-www-form-urlencoded
                  > Connection: Close
                  > User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
                  > Host: www-1.gzentools.com
                  > Content-Length: 67
                  > Pragma: no-cache
                  > Cookie: sm=browser-server
                  > field_textbox=t est+post+data&f ield_hidden=Thi s+is+hidden+fie ld+data[/color]

                  I see, but perhaps the packet sniffer suppressed it, or maybe the server
                  application is smart enough to figure out that the last part must be the
                  data.

                  Anyway, I just found out that correcting the Content-Type solved the problem
                  after all (At first I accidentally used a wrong parameter string when
                  testing that solution).

                  Thanks to everyone in the newsgroup who helped me solving this!

                  Gerard van Wilgen
                  --
                  www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
                  www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
                  tradukvortaro por Windows)

                  Comment

                  Working...