2

I'm trying to send a post variable to the url which I'm redirecting to.

I'm currently using the Get method and sending it like this:

// Redirect to page with url parameter (GET)
Response.Redirect("web pages/livestream.aspx?address="+ ((Hardwarerecorders.Device)devices[arrayIndex]).streamAddress);

And retrieving it like this:

// Get the url parameter here
string address = Request.QueryString["address"];

How do I convert my code to use the POST method?

B.T.W., I don't want to use a form to send the post variable.

M Zeinstra
  • 1,931
  • 4
  • 17
  • 46

2 Answers2

1

Using HttpClient:

To send POST query:

using System.Net.Http;

public string sendPostRequest(string URI, dynamic content)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://yourBaseAddress");

        var valuesAsJson = JsonConvert.SerializeObject(content);
        HttpContent contentPost = new StringContent(valuesAsJson, Encoding.UTF8, "application/json");

        var result = client.PostAsync(URI, contentPost).Result;
        return result.Content.ReadAsStringAsync().Result;
    }

Where 'client.PostAsync(URI, contentPost)' is where the content is being sent to the other website.

On the other website, an API Controller needs to be established to receive the result, something like this:

[HttpPost]
    [Route("yourURI")]
    public void receivePost([FromBody]dynamic myObject)
    {
        //..
    }

However, you might also want to look into using a 307 re-direct, especially if this is a temporary solution.

https://softwareengineering.stackexchange.com/a/99966

Community
  • 1
  • 1
  • Thanks! But how do I sent it? And when the browser arrived at the redirected page, how do I retrieve the post content? – M Zeinstra Mar 11 '16 at 11:46
  • 1
    I misunderstood slightly, the above method simply sends from one location and receives the result back. Updating now. – Andrew Cachia Mar 11 '16 at 11:55
  • Note that 'myObject' can be of any type; string, array, etc. Ideally if you know the type beforehand, its better to declare it. – Andrew Cachia Mar 11 '16 at 12:19
  • Does this only work if the second website is opened in another tab? – M Zeinstra Mar 11 '16 at 12:24
  • Well not necessarily open, but there needs to be another instance of it running. So if you're testing locally it needs to be running on some server such as IIS or Apache. – Andrew Cachia Mar 11 '16 at 12:31
  • No :( It says: "The type or namespace 'HttpClient' could not be found".. I've tried to import the namespace 'System.Net.Http', but that can't find Http.. Any solution for this? – M Zeinstra Mar 14 '16 at 09:47
  • 1
    Install [this](https://www.nuget.org/packages/Microsoft.Net.Http) nuget package, then add a reference to `System.Net.Assembly` – Andrew Cachia Mar 14 '16 at 11:47
  • 1
    Also I can't comment on the other answer by A Biswas, but Get and Post do not work like that! You do not POST from one end and GET it from another, they are two separate protocols! A GET method still sends a request, it just does not contain a body with content in it. – Andrew Cachia Mar 14 '16 at 11:57
  • I suppose, I have to add the reference `System.Net.Http`? If so, it already was added.. – M Zeinstra Mar 14 '16 at 15:49
  • Are you adding reference to the dll by right clicking on the "references" tab and selecting "Add Reference..." ? – Andrew Cachia Mar 15 '16 at 10:12
  • Yes, if this is the right way.. https://gyazo.com/4389a17b73f4cfa1ee13b40c5bac2bf1.png – M Zeinstra Mar 15 '16 at 10:17
  • 1
    And did you install it in the correct project within the solution? The method described in the link is the correct way of referencing HttpClient so you must be doing something wrong... – Andrew Cachia Mar 15 '16 at 12:59
  • Okay, I've tried it in a class with `using System.Net.Http;` and that just works perfect. The only thing is that I want to use it in ASP.Net (while using the language C#), so I have to use `<%@ Import Namespace="System.Net.Http" %>`, but when using that, it won't work.. – M Zeinstra Mar 15 '16 at 13:03
  • Maybe we can discuss about this problem in this chat? https://chat.stackoverflow.com/rooms/106372/send-an-receive-a-post-variable – M Zeinstra Mar 15 '16 at 14:48
  • I don't have enough stack overflow points yet to chat :( Send me your email or something on the chat (I can still see messages you write), and I'll message you – Andrew Cachia Mar 15 '16 at 18:25
  • Hmm how many points do you need? I can give you 15 points for accepting the answer? – M Zeinstra Mar 15 '16 at 18:26
  • Well if you think the answer is helpful you could! I would be grateful haha. I should have enough then – Andrew Cachia Mar 15 '16 at 18:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106389/discussion-between-andrew-cachia-and-m-zeinstra). – Andrew Cachia Mar 15 '16 at 19:16
  • I'm in the chat again :) – M Zeinstra Mar 24 '16 at 13:09
1
using System.Net.Http;

POST

using (var client = new HttpClient())
{
    var values = new Dictionary<string, string>
    {
       { "thing1", "hello" },
       { "thing2", "world" }
    };

    var content = new FormUrlEncodedContent(values);

    var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

    var responseString = await response.Content.ReadAsStringAsync();
}

GET

using (var client = new HttpClient())
{
    var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx");
}

My Personal Choice is Restsharp it's fast but for basic operations you can use this

A Biswas
  • 421
  • 6
  • 12
  • @A But one thing I don't understand.. How do I redirect to the given page with the given post variables and retreive them IN the redirected page? – M Zeinstra Mar 11 '16 at 11:53
  • 1
    Just use the "get" code in the page you want the data to be fetched , your data will be saved in the responseString – A Biswas Mar 13 '16 at 02:27
  • It says: "The type or namespace 'HttpClient' could not be found".. I've tried to import the namespace 'System.Net.Http', but that can't find Http.. Any solution for this? – M Zeinstra Mar 14 '16 at 09:28
  • Add this `using System.Net.Http;` – A Biswas Mar 15 '16 at 09:04