在這裏,我從我的Web應用程序調用Restful WCF服務,我不知道如何傳遞一個對象作爲參數。這裏是我的客戶端代碼:如何在HttpWebRequest POST中傳遞一個對象作爲參數?
protected void Button1_Click(object sender, EventArgs e)
{
UserInputParameters stdObj = new UserInputParameters
{
AssociateRefId = "323",
CpecialLoginId = "[email protected]",
PartnerId = "aaaa",
FirstName = "aaaa",
LastName = "bbbb",
Comments = "dsada",
CreatedDate = "2013-02-25 15:25:47.077",
Token = "asdadsadasd"
};
string url = "http://localhost:13384/LinkService.svc/TokenInsertion";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
System.Net.WebRequest webReq = System.Net.WebRequest.Create(url);
webReq.Method = "POST";
webReq.ContentType = "application/json; charset=utf-8";
DataContractJsonSerializer ser = new DataContractJsonSerializer(stdObj.GetType());
StreamWriter writer = new StreamWriter(webReq.GetRequestStream());
writer.Close();
webReq.Headers.Add("URL", "http://localhost:13381/IntegrationCheck/Default.aspx");
System.Net.WebResponse webResp = webReq.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(webResp.GetResponseStream());
string s = sr.ReadToEnd().Trim();
}
catch (Exception ex)
{
}
}
我的服務方法:
public string UserdetailInsertion(UserInputParameters userInput)
轉換你stdObj使用JSON.NET或任何其他LIB成JSON,寫在你的poststream(在這種情況下,作家) – 1Mayur 2013-02-25 12:25:08