Hi!
I'm trying to send data from a mobile application written in Java ME, to an ASP.NET website connected to an SQL server. I tried to open an httpconnection output stream in the Java ME application, and send the data in a POST to the website. The problem is I don't know how to accept data on the website. I found code online that I tried to modify to suit my application, but I feel I'm waaaay off. Could anyone give me some pointers on how to go about this?

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

        Dim str As Stream
        Dim strLen As Integer
        Dim strRead As Integer


        'Request input stream from the poster
        str = Request.InputStream


        'Get the length of incoming string
        While (strLen = 0)
            strLen = str.Length
        End While

        'make a Byte array the size of the stream
        Dim strArr(strLen) As Byte


        'read the stream into the array
        strRead = str.Read(strArr, 0, strLen)

        Label1.Text = strArr.ToString()


    End Sub

Dani AI

Generated

Good progress by — reading the incoming entity body is a valid way to accept a POST — and ’s suggestion to expose a service is the right long-term design when multiple clients or formats are involved.

Two practical server-side patterns are common. If the phone posts form data (Content-Type: application/x-www-form-urlencoded or multipart/form-data), ASP.NET parses those into the Request.Form (and Request.Files) collections so named fields can be read directly. (learn.microsoft.com)
If the phone sends a raw payload (JSON, XML or binary), read the raw body from Request.InputStream and decode with the request’s ContentEncoding (avoid trying to poll InputStream.Length). StreamReader(Request.InputStream, Request.ContentEncoding).ReadToEnd() is the usual, reliable approach for text payloads. (learn.microsoft.com)

An unobtrusive example (C#) showing both approaches:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.HttpMethod == "POST")
    {
        // form field:
        string name = Request.Form["name"];

        // raw body:
        string body;
        using (var reader = new System.IO.StreamReader(Request.InputStream, Request.ContentEncoding))
        {
            body = reader.ReadToEnd();
        }

        // process 'name' or 'body' as appropriate
    }
}

Check the HTTP verb before reading the body. (learn.microsoft.com)

For a production endpoint consider a lightweight HTTP handler (.ashx) or a Web API controller instead of stuffing logic into a page — handlers are simpler for raw POST/REST endpoints and avoid page-lifecycle overhead. Also: do not busy-wait on stream length, require HTTPS, validate/sanitize input, avoid writing raw client data to files in production, and use tools like Fiddler or curl to confirm the phone’s headers (Content-Type, Content-Length) and payload. (learn.microsoft.com)

Recommended Answers

All 5 Replies

Member Avatar for Member #905211

Why not save the data in the database and display that data on the ASP.Net page? You can then have an AJAX web service that loops to see if there is new data in the database to display.

You mean send the data directly from the mobile phone to the SQL server? I didn't look into that because in other forums I saw people asking about that, and everyone would reply, you'll need to add a webpage with that. Is it doable?

Member Avatar for Member #905211

Just create a web service. the web service will be able to be used by any language that supports web services and any device that has an internet connection. You will be able to send data through to the database and use another web service to grab the data.

Ok I'm sorry, but I'm new to this and I'm really confused. I've already written the Java ME application that sends data using an HTTP post, and I've already made the website using ASP.NET. So I'd prefer not to get into web services. Forget the SQL server, where in my ASP.NET application should I accept the data coming from the mobile phone? It doesn't really make sense in the page_load event? Because that doesn't seem right to me.

In case someone ever has the same problem, turns out the above code is pretty much right. I added these lines of code to make it work better:

' the stream will be ASCII encoded'
         Dim ascii As ASCIIEncoding = New ASCIIEncoding

        'Get ASCII into reg. string here'
         strmContent = ascii.GetString(strArr)
         Label1.Text = strArr.ToString()

        'write the received data to a text file'
        Dim FILE_NAME As String = "C:\\NP\\received.txt"
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
        objWriter.WriteLine(strmContent)
        objWriter.WriteLine()
        objWriter.Close()

Most of the code I used came from this forum discussion, if you'd like to look at it: http://bytes.com/topic/asp-net/answers/327711-receive-https-post
And the code should be placed in the ASP.NET page-load event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.