-2

Does anyone know a simple way to send an int, a string, or a byte[] from a Windows Form Application via 'POST' method and get this data in an ASP.NET WebForm?

So, in fact:

Form1 (data) -→ (data) WebPage1.aspx

Here is what I have now in my code : Client side :

    String idtostring = "id=" + opid.ToString();
    intarray = Encoding.ASCII.GetBytes(idtostring);
    startlog(intarray);

    private void startlog(byte[] array)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:51146/MyPage.aspx");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = array.Length;
        var Url = "http://localhost:51146/MyPage.aspx";
        wb.Navigate(Url, true);
        Stream postdata = request.GetRequestStream();
        postdata.Write(array, 0, array.Length);
        postdata.Close();
    }

Server side :

    private String getpostdata()
    {
        return Request.Form["id"];
    }

And this returns null... I don't know whether the problem is on client side or on server side. What I need to do is send from the client a data which looks like "id=7" via POST method and get it back on the server. I'm using the id I get to custom authenticate an user so I need to show in the Web Browser the session where the user will be authenticated.

UPDATE : I'm not sure Request.Form is the right place to watch...

See this image which shows what I got with a breakpoint (I'm new so I cannot post images for the moment) → https://i.stack.imgur.com/3VTyL.png

I just tried with this :

    private void startlog(byte[] array)
    {
        var Url = "http://localhost:51146/MyPage.aspx";
        wb.Navigate(Url, "_blank", array, "");
    }

And the result is exactly the same...

LeReferee
  • 115
  • 1
  • 9
  • Well, web forms can read form data. And Win Forms has libraries to post data. What have **you** tried? Because the way you've written your question, you're basically asking us to write the code for you. And that's not what we're here for. – mason Apr 24 '15 at 13:50
  • I've tried what is at this place : http://stackoverflow.com/questions/29842443/retrieving-post-data-in-c-sharp-asp-net But due to the large amount of questions I didn't get any more option after one hour, so I needed to get this up... – LeReferee Apr 24 '15 at 13:59
  • *Show your work* in your post. Then we can help correct any issues. Describe how your attempt isn't working. – mason Apr 24 '15 at 14:01
  • I'm editing it progressively so you can see wihat I've been trying ;) – LeReferee Apr 24 '15 at 14:03
  • You're just writing the bytes to the request, but you aren't including them in the `x-www-form-urlencoded` format. Instead of writing the bytes to the request, convert them to a Base 64 string and embed that string as part of the `x-www-form-urlencoded`. On the server side, convert from a base 64 string to a byte array. You picked the most difficult data type to start with: string would be much easier to understand the concept of posting form data to a server. Once you understand that, then you can move onto int and then byte arrays. – mason Apr 24 '15 at 14:05
  • See [this answer](http://stackoverflow.com/a/22234455/1139830) for how to add form data to a web request. – mason Apr 24 '15 at 14:08
  • Okn thank you for the advice :) But I didn't understand what you meant in "embedding the string as part of the `x-www-form-urlencoded`"... – LeReferee Apr 24 '15 at 14:09
  • OK, in fact, sending the request works just fine with the `bytes[]`... What doesn't work in retrieving the data from the ASP Page. I've no idea what's wrong exactly with my getpostdata function... – LeReferee Apr 24 '15 at 14:45
  • Are you **sure** that sending it works just fine with the bytes[]? Because I don't see you properly embedding that data as part of the form data, which explains why the server can't pull the bytes out of the form. Read my last comments. – mason Apr 24 '15 at 14:47
  • And how do I embed those bytes exactly ?... If that's my mistake, I'd like to have an idea about how to correct it... (Excuse stress and coffee effect for my being nervous ^^) – LeReferee Apr 24 '15 at 15:24
  • Skip the bytes for now. Start with just a string. Read the answer I linked to above, it shows how to add form data. Once you have gotten that working, then you can look at doing bytes by converting them to Base 64. But only do that once you've got strings working. Start simple, then get harder. – mason Apr 24 '15 at 15:26
  • Problem is it seems that I cannot use HttpUtility from a WinForm... Since I cannot even resolve the "missing assembly reference". – LeReferee Apr 24 '15 at 15:31
  • If you have a problem, by all means say what the problem is so I don't have to guess. Are you getting an error message? What steps have you taken with HTTP Utility? What happens if you manually build up your `x-www-form-urlencoded` with `HttpWebRequest`? – mason Apr 24 '15 at 15:34
  • The problem is, HttpUtility is normally in System.Web namespace, but I think this only applies to WebForms since VIsual Studio doesn't understand what HttpUtility is (I got the "using System.Web;" instruction at the beginning of my file but the IDE keeps telling me "Name 'HttpUtility' does not exist in the current context"). As the solution you're proposing needs this HttpUtility, I'm not able to use it for the moment I guess... – LeReferee Apr 24 '15 at 15:38
  • [HttpUtility](https://msdn.microsoft.com/en-us/library/system.web.httputility(v=vs.100).aspx) is part of the .NET Framework. It is not Web Forms specific. You can add a reference to `System.Web` assembly in your Win Forms project. – mason Apr 24 '15 at 15:41
  • This reference is added, so that seems really weird... EDIT Nevermind, i confused with the "using" instruction – LeReferee Apr 24 '15 at 15:43
  • Fine. You don't have to use `HttpUtility`, you can manualy encode the values. See [Parisa's answer](http://stackoverflow.com/a/29618904/1139830). – mason Apr 24 '15 at 15:45
  • This answer was roughly the code I had (since my `byte[]` value was assigned like this : `array = Encoding.UTF8.GetBytes(idtostring);` I just did not had the two "Response" lines – LeReferee Apr 24 '15 at 15:56

1 Answers1

1

Here's a string example.

var helloWorldString="HelloWorldKey=HelloWorldValue";
var array= Encoding.ASCII.GetBytes(helloWorldString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:51146/MyPage.aspx");
request.Method = "POST";
request.ContentLength = array.Length;
request.ContentType = "application/x-www-form-urlencoded";
var Url = "http://localhost:51146/MyPage.aspx";
using(Stream postdata = request.GetRequestStream())
{
    postdata.Write(array, 0, array.Length);
}

On the server you'd read it with Request.Form["HelloWorldKey"];

ints are trickier. Convert them to strings on the client, then parse them to ints on the server.

Same thing with byte arrays. Convert them to Base 64 strings on the client, then convert them back to byte arrays on the server.

It would be better to use HttpUtility to encode the string values, rather than manually doing it. See this answer for details.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • That's almost what I had, just that my String was just my int.toString(). So I changed it by what you proposed, which, adapted, gives `"id=" + id.ToString();` . I tried getting it back on the server but Request.Form["id"] is null... But maybe I'm not doing it well I have now this on my server : `private String getpostdata(){ return Request.Form["id"]; }` – LeReferee Apr 24 '15 at 16:07
  • @LeReferee And clearly that wouldn't work, because you have to provide it in the format of `application/x-www-form-urlencoded`. Go read up on that format since you're trying to use it. It's key/value pairs. – mason Apr 24 '15 at 16:08
  • But the `Request.Form["key"];` part was a huge part of what I needed :D – LeReferee Apr 24 '15 at 16:12
  • @LeReferee I can't tell you what you're doing wrong if you don't show me the code you're using. You can edit it into your question. – mason Apr 24 '15 at 16:49
  • I wrote the code I used for getpostdata() just before ;) `private String getpostdata(){ return Request.Form["id"]; }` – LeReferee Apr 27 '15 at 07:26