On Feb 20, 12:01 am, [EMAIL PROTECTED] wrote:
> Hi,
>
> Can you please tell me how can I get the HTTP Post data from a http
> observer?
>
> Thank you.

I did some research, and come up the following code.
But when i read the UploadStream, it always return 0.  I have checked
via etheral, there is post data, but my code just is not able to read
it.

Can you please tell me what did I do wrong?

here is an overview of what I did:
1. cast the observer subject to be a nsIUploadChannel
2. get the Upload input Stream from the uploadChannel
3. read from the upload input stream.

But i just get 0 when I read from the stream.

        nsCAutoString requestMethod;
        nsCOMPtr<nsIHttpChannel> channel =
do_QueryInterface(aSubject);

                channel->GetRequestMethod(requestMethod);

                FILE* log = fopen( "http.log", "a+");

                if (requestMethod.Equals("POST") ) {

                  nsCOMPtr<nsIUploadChannel> uploadChannel =
do_QueryInterface(aSubject);

                  if (uploadChannel) {
                    nsCOMPtr<nsIInputStream> is;
                    uploadChannel->GetUploadStream(getter_AddRefs(is));

                    if(is) {
                      nsresult rv2;

                      PRUint32 numRead;
                      char buf[512];
                      fprintf (log, "%s POST ",uri);
                      while (1)
                        {
                          rv = is->Read(buf, sizeof(buf), &numRead);
                          if (NS_FAILED(rv))
                            {
                              printf("### error reading stream: %x\n", rv);
                              fprintf (log, " error ");
                              break;
                            }

                          if (numRead == 0) {
                            fprintf (log, " nothing ");
                            break;
                          }

                          buf[numRead -1]= '\0';
                          // buf now contains numRead bytes of data
                          fprintf (log, "%s",buf);
                        }

                      fprintf (log, "\n");
                    }

                  } else {

                    fprintf (log, "%s POST \n",uri);
                  }

                } else {
                  fprintf (log, "%s\n",uri);
                }

                fclose(log);
_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network

Reply via email to