Results 1 to 1 of 1

Thread: [RESOLVED] HttpWebRequest: POST to HttpContext 'Form' collection

  1. #1

    Thread Starter
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Resolved [RESOLVED] HttpWebRequest: POST to HttpContext 'Form' collection

    I'm struggling to pass data through an HttpWebRequest object in a Windows application to show up as Form variables in the HttpContext.Request.Form collection on an ASP.NET server in a generic ASHX handler.

    Windows app posting code:

    Code:
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    	' POST
    	Dim sendString As String = "{""coilid""=""Ax1223""}"
    	Dim wr As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create("http://10.20.120.36/ajax/coilbucket.ashx"), Net.HttpWebRequest)
    	wr.KeepAlive = False
    	wr.Method = "POST"
    	wr.ContentType = "application/json; charset=utf-8;"
    	Using sw As IO.StreamWriter = New IO.StreamWriter(wr.GetRequestStream)
    		sw.Write(sendString)
    	End Using
    	' READ RESPONSE
    	Dim res As Net.WebResponse = wr.GetResponse()
    	Dim str As New IO.StreamReader(res.GetResponseStream, System.Text.Encoding.UTF8)
    	Dim s As String = str.ReadToEnd()
    	Debug.WriteLine(s)
    End Sub
    The code, above, formats the POST as a JSON object, which it appears to do correctly, but am unable to read the request in neither the Form collection or the queryString collection of the HttpContext.Request object.

    The ASP.NET code:

    Code:
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    	Dim d As New Dictionary(Of String, Object)(StringComparer.OrdinalIgnoreCase)
    	'
    	Dim sb As New System.Text.StringBuilder
    	For Each s As String In context.Request.Form
    		If sb.Length > 0 Then sb.Append(",")
    		sb.Append(s & "=" & context.Request.Form.Item(s))
    	Next
    	d.Item("forms") = sb.ToString
    	sb.Clear()
    	For Each s As String In context.Request.QueryString
    		If sb.Length > 0 Then sb.Append(",")
    		sb.Append(s & "=" & context.Request.QueryString.Item(s))
    	Next
    	d.Item("querystring") = sb.ToString
    	' This function simply converts a dictionary to a JSON string
    	Dim result As String = App.JSONString(d)
    	'
    	' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    	context.Response.ContentType = "text/plain"
    	context.Response.Write(result)
    	' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    End Sub
    The result is:

    {"forms":"","querystring":""}

    When performing the same POST from JQuery, the Forms collection is populated:

    {"forms":"coilid=254879","querystring":""}

    (Understandably, the QueryString is not populated - I can do that by adding a query string to the URI).

    JQuery AJAX Call:

    Code:
    $.ajax({
    	url: '/ajax/coilbucket.ashx',
    	type: 'POST',
    	dataType: 'json',
    	data: { coilid: id },
    	beforeSend: function () {
    		...
    	},
    	success: function (data, textStatus, xhr) {
    		...
    	},
    	error: function (xhr, textStatus, errorThrown) {
    		...
    	}
    });
    So, how do I get the POST data as a JSON representation to show up in the Forms collection from a windows application just like the JQuery AJAX call?

    EDIT: SOLVED.

    The JQuery .ajax() properties were setup incorrectly - the contentType was not specifically set, so was default (x-www-form-urlencoded) which, while worked, was not the understood behavior. The data populated the forms collection. By sending the data as 'application/json' as the content type from the windows application AND the JQuery ajax call, neither the forms collection or the queryString are populated. The JSON string must be parsed through context.Request.InputStream.

    With 'x-www-form-urlencoded', the Form collection is populated, but the posted data must be of a url encoded string, and not JSON. I actually had a mistake in my .ajax() call which gave me what I wanted - a 'bug which worked' - but was only brought to light when accessing the handler from a .NET windows application.

    I don't know if anyone understood this problem, but I got it figured out, so it's here for posterity.
    Last edited by SJWhiteley; Sep 24th, 2015 at 02:02 PM. Reason: Added 'solution'
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width