Groups > Borland > Dellphi web services soap > Re: HTTP Post and Web Service




HTTP Post and Web Service

HTTP Post and Web Service
Thu, 13 Mar 2008 13:01:41 -050
I have a working .NET web service (written in Delphi 2005) which
accepts a single XML document and some processing parameters from
clients as strings.  The web service does the necessary processing and
then gives the client back the required answer.

Great!  (Going on two years of relatively problem-free deployment.)

I have a potential client who has no interest in dealing with SOAP and
web services.  This client only wants to use HTTP Post to send a form
encoded variable named xml to get the info to me, and then wants me to
HTTP Post the answer back to a url.

Since I already have the web service written and it does everything I
need it to, how can I "accept" an http post to my web service,
extract
the xml, process what I need to process, and then http post the answer
back to this client?

Is this even possible?

I've been fooling around for a couple of hours using a
System.Net.HTTPWebRequest object and a System.net.WebResponse object
to try and post an xml to my web service with absolutely no luck. (All
I am getting is a http 500 - Internal Server Error)

Any ideas?

Post Reply
Re: HTTP Post and Web Service
Thu, 13 Mar 2008 18:43:33 -040
On Thu, 13 Mar 2008 13:01:41 -0500, Jano wrote:

> Since I already have the web service written and it does everything I
> need it to, how can I "accept" an http post to my web service,
extract
> the xml, process what I need to process, and then http post the answer
> back to this client?

So where are you having problems? Accepting the posted data or sending
it back? The easiest way to accept a page post is simply to write an
ASPX page or if you are more adventurous a webahndler (ASHX page).

-- 
Marc Rohloff [TeamB]
Post Reply
Re: HTTP Post and Web Service
Thu, 13 Mar 2008 20:41:53 -050
Mostly, I am having trouble "HTTP Posting" to my web service just to
make sure it worked the way I thought it should.  

Here's what I tried (Based on this thread):

http://forums.asp.net/p/1056508/1506225.aspx


The method (HTTPPostTest) I am trying to post to accepts a single
string named XMLString.  All I'm getting are "500 - Internal Server
Errors."  If I try with the methods generated by Delphi via the WSDL,
everything is ok.  But, my client has no interest in using SOAP
envelopes and the WSDL.  And since he's paying, I don't really have
the leverage to tell him to do it or else. <g> 

On the webserver, I'm seeing the http post on port 443 from my ip to
the webserver ip to /WebService/Application.asmx, but without anything
about the method HTTPPostTest.  Don't know it that means anything.

I haven't even gotten around to trying to send the results, since I
can't even get a test case to work on my local machine.  Help!  Where
am I screwing up and what am I missing?


var
  AByteArray: array of System.Byte;
  AnEncoding: System.Text.Encoding;
  AStream: Stream;
  AStreamReader: StreamReader;
  AUri: Uri;
  AWebRequest: System.Net.HttpWebRequest;
  AWebResponse: System.Net.WebResponse;
  Result: string;
begin
  AnEncoding := nil;
  AStream := nil;
  AStreamReader := nil;
  AUri := nil;
  AWebRequest := nil;
  AWebResponse := nil;
  try
    AByteArray := System.Text.Encoding.ASCII.GetBytes('XMLString=' +
rtbXMLString.Text);
      AUri :=
Uri.Create('https://www.mydomain.com/WebService/Application.asmx/HTTPPostTest');

      AWebRequest :=
HttpWebRequest(System.Net.WebRequest.CreateDefault(AUri));
      AWebRequest.Method := 'Post';
     

      AWebRequest.ContentLength := Length(AByteArray);
      AWebRequest.ContentType := 'application/x-www-form-urlencoded';

      AStream := AWebRequest.GetRequestStream();
      AStream.Write(AByteArray, 0, Length(AByteArray));
      AStream.Flush;

      //Send the data and get the response
      AWebResponse := AWebRequest.GetResponse();

      //Get the stream of data (response) from the WebRequest object
      AStream.Close;
      AStream := nil;
      AStream := AWebResponse.GetResponseStream();

      //Change stream up to a StreamReader object and read the results
      AnEncoding := System.Text.Encoding.GetEncoding('utf-8');
      AStreamReader := StreamReader.Create(AStream, AnEncoding);
      Result := AStreamReader.ReadToEnd();

      //Release the stream and response resources
      AStreamReader.Close;
      AWebResponse.Close;

      rtbResult.Text := Result; 
  except
    //Exception handling, eventually
  
  end;
end;

Thanks!!


On Thu, 13 Mar 2008 18:43:33 -0400, "Marc Rohloff [TeamB]"
<marc@nospam.marcrohloff.com> wrote:

>On Thu, 13 Mar 2008 13:01:41 -0500, Jano wrote:
>
>> Since I already have the web service written and it does everything I
>> need it to, how can I "accept" an http post to my web
service, extract
>> the xml, process what I need to process, and then http post the answer
>> back to this client?
>
>So where are you having problems? Accepting the posted data or sending
>it back? The easiest way to accept a page post is simply to write an
Post Reply
Re: HTTP Post and Web Service
Fri, 14 Mar 2008 08:13:59 -040
On Thu, 13 Mar 2008 20:41:53 -0500, Jano wrote:

> Mostly, I am having trouble "HTTP Posting" to my web service just
to
> make sure it worked the way I thought it should.  

Have you tried using a simple string instead of XML? If you use XML
you are going to need to encode the XML first:

AByteArray := System.Text.Encoding.ASCII.GetBytes('XMLString=' +
  HttpUtility.HtmlEncode(rtbXMLString.Text) );
  
Lastly you are probably closing AStream far too early.

-- 
Marc Rohloff [TeamB]
Post Reply
Re: HTTP Post and Web Service
Fri, 14 Mar 2008 10:00:15 -050
Still a 500 - Internal Server Error after adding the
HttpUtility.HtmlEncode and moving the AStream.Close to the end of the
procedure.

Is there a way I can debug the web service when the Post happens so I
can see if any part of the method is being executed at all?

Thanks for the reply and advice.  I'll keep trying. :)

On Fri, 14 Mar 2008 08:13:59 -0400, "Marc Rohloff [TeamB]"
<marc@nospam.marcrohloff.com> wrote:

>On Thu, 13 Mar 2008 20:41:53 -0500, Jano wrote:
>
>> Mostly, I am having trouble "HTTP Posting" to my web service
just to
>> make sure it worked the way I thought it should.  
>
>Have you tried using a simple string instead of XML? If you use XML
>you are going to need to encode the XML first:
>
>AByteArray := System.Text.Encoding.ASCII.GetBytes('XMLString=' +
>  HttpUtility.HtmlEncode(rtbXMLString.Text) );
>  
Post Reply
about | contact