how to post a form use HTTP librarys

nsc

New Member
this form use Image Verification
need some code samples.
* bin is Binary file library , bit is Bitwise , obj is Object

B4X:
Sub Globals
   'Declare the global variables here.
   Dim buffer(0) As Byte
End Sub

Sub App_Start
   Form1.Show
End Sub


Sub Button1_Click
  textbox1.Text="Post gogogo"
  bit.New1
   Request.New1("http://www.site.com/login")
   Request.Method = "POST"
   Request.ContentType = "application/x-www-form-urlencoded"
   Request.Timeout = 30000
  s="username=nsc&password=XXX&useCookie=1&signonForwardAction=&validcode=" & textbox2.Text
   buffer() = bit.StringToBytes(s,0,StrLength(s))
   Request.ContentLength=StrLength(s)
   bin.New1(Request.GetStream,True)
   bin.WriteBytes(buffer())
   Response.New1
   Response.Value = Request.GetResponse
   textbox1.Text = Response.GetString
   Response.Close
End Sub

Sub Button2_Click
    Form1.Show
    Request.New1("http://www.site.com/valid")
    Response.New1
    obj.New1(False)
    args.New1(1)  
    Response.Value = Request.GetResponse
    args.SetObject(0,Response.GetStream) '
    obj.CreateNew2("System.Drawing.Bitmap" & obj.System_Drawing,args.Value)
    image1.Image = obj.Value
End Sub

run it , button2 click , it's works , I got a validcode image
but button1 click , I got error , The remote server returned an error (417) Expectation Failed
 
Last edited:

nsc

New Member
:BangHead:
lighttpd, .NET, HttpWebRequest

Yesterday, when I deployed the server for my PocketPC-Application to an environment running lighttpd and PHP with FastCGI SAPI, I found out that the communication between the device and the server didn’t work.

All I got on the client was an Exception because the server sent back error 417: Precondition failed.

Of course there was nothing in lighttpd’s error log, which made this a job for EtherealWireshark.

The response from the server had no body explaining what was going on, but in the request-header, something interesting was going on:

Expect: 100-continue

Additionally, the request body was empty.

It looks like HttpWebRequest, with the help of the compact framework’s ServicePointManager is doing something really intelligent which lighttpd doesn’t support:

By first sending the POST request with an empty body and that Expect: 100-continue-header, HttpWebRequest basically gives the server the chance to do some checks based on the request header (like: Is the client authorized to access the URL? Is there a resource available at that URL?) without the client having to transmit the whole request body first (which can be quite big).

The idea is that the server does the checks based on the header and then either sends a error response (like 401, 403 or 404) or it advises the client to go ahead and send the request body (code 100).

Lighttpd doesn’t support this, so it sends that 417 error back.

The fix is to set Expect100Continue of System.Net.ServicePointManager to false before getting a HttpWebRequest instance.

That way, the .NET Framework goes back to plain old POST and sends the complete request body.

In my case that’s no big disadvantage because if the server is actually reachable, the requested URL is guaranteed to be there and ready to accept the data on HTTP-level (of course there may be some errors on the application level, but there has to be a request body for them to be detected).

This entry was posted on Wednesday, September 13th, 2006 at 10:43 and is filed under Programming, Solutions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
3 Responses to “lighttpd, .NET, HttpWebRequest”

1. Steve Greenland Says:
October 21st, 2006 at 19:11

Thanks for this post — I’d figured out the 100-Continue problem with lighttpd (it is a documented bug, but hard to fix with the current code. I’d guess that the 1.5 re-do might fix this, or at least make it possible.) What I’d not been able to find was the magic knob in .NET (or rather, Mono) to bypass the Expect: header. So, again, thanks.

It’s worth noting that not using the request header should *never* break anything - the server is still allowed to reject the POST for any reason. All the request header does is allow the optimization of not sending the POST body until the basic request has been checked.

B4X:
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.ServicePoint.Expect100Continue = false;
 
Top