ASP.net Web API to implement post message construction and push

Source: Internet
Author: User
Tags empty oauth port number visual studio

Bishi is related to the OAuth protocol, and the HTTP Get/post method must be understood to understand the OAuth protocol. So we studied how to construct post messages using Web APIs or MVC and implement client-server interaction.

The tool I use is Visual Studio 2013 + Web API 2 + MVC 5.

Create a new two Web project in two different VS2013 instances, choosing an empty template, one named client, an MVC schema, and the other named server, with the Web API architecture.

Two different VS2013 instances are required to enable two IIS express services to run concurrently on the same machine.

Let's take a look at the client first:

Because it's just a demo, we'll just create a new empty MVC controller in the client and name it HomeController. It will bring an index () method itself. We're here to write the code that constructs the request and sends the request:

Copy Code

Namespace Client.controllers

{

public class Homecontroller:controller

{

//

Get:/home/

Public ActionResult Index ()

{

String url = ""; Here we do not know the URL of the server, so leave it blank

#region Construct Post Requests

HttpWebRequest request = webrequest.create (URL) as HttpWebRequest;

Request. method = "POST";

Request. ContentType = "application/x-www-form-urlencoded";

Request. Host = "localhost:14340";

String BODY = "client_id=123&redirect_uri=" +url+ "&response_type=code";

byte[] Bodybytes = Encoding.UTF8.GetBytes (body);

Stream requeststream = Request. GetRequestStream ();

requestStream.Write (bodybytes,0,bodybytes.length);

#endregion

#region send a request and get a response

The HttpWebRequest.GetResponse () method must be used for the request to be sent and responded to

HttpWebResponse response = Request. GetResponse () as HttpWebResponse;

StreamReader sr = new StreamReader (response. GetResponseStream ());

Viewbag.response = Sr. ReadToEnd ();

#endregion

return View ();

}

}

}

Copy Code

Next we construct the server side:

In the server project, create a new Web API 2 controller. Name Random (I'm named Apitestcontroller here), write a method post in the controller, return the Httpresponsemessage type.

I always thought that the post parameters could be obtained in the request property, but examined all the properties and methods of the request property, including the extension method, without finding a property or method that could directly read the post message request body. It is found that if you want to get the contents of the request body in the POST request, you must encapsulate the parameters in a class. So we set up a class Requestargs as follows:

Copy Code

Namespace Server.models

{

///

For post requests, you must establish a model class for all parameters of the request body.

///

public class Requestargs

{

public string client_id {get; set;}

public string Redirect_uri {get; set;}

public string Response_type {get; set;}

}

}

Copy Code

The parameter here and the request message content in the client action method above must be consistent in name (including case)

We then write to the controller post method as follows:

Copy Code

Namespace Server.controllers

{

public class Apitestcontroller:apicontroller

{

Public httpresponsemessage Post (Requestargs args)

{

string client_id = args.client_id;

string Redirect_uri = Args.redirect_uri;

string response_type = Args.response_type;

Return Request.createresponse (Httpstatuscode.ok, "I have received your message." client_id = "+client_id+", Redirecturi = "+redirect_uri+" Response_type = "+response_type");

}

}

}

Copy Code

Here we return directly to the contents of the message, and in the actual project we can handle the incoming parameters ourselves.

Then we compile the server side (preferably press F5 to run into debug mode). You can see that the Web page appears to be an error page, but regardless of it, our server has already started running.

Note the address of the server (usually the port number will change), and populate the server's address with the client's URL variable.

The compiler runs the client, and if the returned information (here is "I have received your message" and parameter information), it means that our request was successful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.