POST request JSON format data to WCF service

Source: Internet
Author: User
Tags json

Test entity classes: (entities with the same field name need to be built on the client and service side)

public class Compositetype
{
Public Compositetype ()
{
Subcompositetypes = new List<subcompositetype> () {New Subcompositetype ()};
}
public bool Boolvalue {get; set;}
public string StringValue {get; set;}
Public list<subcompositetype> Subcompositetypes
{
Get
Set
}
}
public class Subcompositetype
{
bool Boolvalue = true;
String stringvalue = "Hello";
public bool Boolvalue
{
get {return boolvalue;}
set {boolvalue = value;}
}
public string StringValue
{
get {return stringvalue;}
set {stringvalue = value;}
}
}

Client Request Code:


#region Josnpost
Compositetype Compositetype = new Compositetype
{
StringValue = "1",
Boolvalue = False
};

DataContractJsonSerializer Dcserializer = new DataContractJsonSerializer (typeof (Compositetype));

MemoryStream stream = new MemoryStream ();

Dcserializer.writeobject (stream, Compositetype);

String data = Encoding.UTF8.GetString (stream. ToArray (), 0, (int) stream. Length);

HttpClient client = new HttpClient ();

Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/json"));

String link = "Http://localhost:1766/Service1.svc/CreateUser";

Httpresponsemessage Respondse = await client. Postasync (link, new stringcontent (data));

String Datawithjason = await Respondse. Content.readasstringasync ();

var request = (HttpWebRequest) webrequest.create (new Uri (link));
Request. ContentType = "Application/json";
Request. method = "POST";

using (var requeststream = await request. Getrequeststreamasync ())
{
var writer = new StreamWriter (requeststream);
Writer. Write (data);
Writer. Flush ();
}

using (var resp = await request. Getresponseasync ())
{
using (var responsestream = resp. GetResponseStream ())
{
var reader = new StreamReader (responsestream);
var result = reader. ReadToEnd ();
}
}



Service-side Interface definition:

[OperationContract]
[WebInvoke (
UriTemplate = "CreateUser",
Requestformat = Webmessageformat.json,
Responseformat = Webmessageformat.json,
method = "POST")]
String CreateUser (Compositetype compositetype);



Service-side Interface implementation:

public string CreateUser (Compositetype compositetype)
{
Return "OK" + Compositetype.stringvalue + "" + Compositetype.boolvalue + "" + CompositeType.SubCompositeTypes.FirstOrDef Ault (). StringValue;
}

Because the server needs to implement rest services, it needs to be reconfigured in Web.config (cover ServiceModel):


<system.serviceModel>
<services>
<service behaviorconfiguration= "Myservicebehavior" name= "Server.service1" >
<endpoint address= "" binding= "WebHttpBinding" behaviorconfiguration= "web" contract= "Server.iservice1" >
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
<endpoint address= "Max" binding= "mexHttpBinding" contract= "IMetadataExchange" ></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name = "Web" >
<webhttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name= "Myservicebehavior" >
<!--to avoid disclosing metadata information, set the values below to false before deployment
<servicemetadata httpgetenabled= "true" httpsgetenabled= "true"/>
<!--to receive exception details in faults for debugging purposes and set the value below to true. Set to false before deployment to avoid disclosing exception information
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding= "basichttpsbinding" scheme= "https"/>
</protocolMapping>
<servicehostingenvironment aspnetcompatibilityenabled= "true" multiplesitebindingsenabled= "true"/>
</system.serviceModel>


Tested, also available in Windows Runtime projects.

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.