Flipper 我用你的模板写了一个服务器代码
[ServiceContract]
public class MyServer
{
public void Start()
{
Task.Factory.StartNew(() =>
{
WebServiceHost ws = new WebServiceHost(this.GetType(), new Uri("http://0.0.0.0/Test"));
ws.Open();
});
}
[OperationContract]
[WebInvoke(UriTemplate = "SaveUsersCode", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string SaveUsers(UserCode code)
{
return "GOT: " + code.companyName + "," + code.imsi;
}
public class UserCode
{
public string companyName;
public string imsi;
}
}
并将其称为
//Start server
var m = new MyServer();
m.Start();
Task.Delay(1000);
//Call server method
using (var wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
var obj = new { companyName = "cocaCola",imsi="3324" };
string response = wc.UploadString("http://localhost/Test/SaveUsersCode", new JavaScriptSerializer().Serialize(obj));
Console.WriteLine(response);
}
达达,它的作品