我尝试将一些数据发布到Web服务,这是我要发布的对象:
public class TSep
{
public string noKartu { get; set; }
public string tglSep { get; set; }
public string tglRujukan { get; set; }
public string noRujukan { get; set; }
public string ppkRujukan { get; set; }
public string ppkPelayanan { get; set; }
public string jnsPelayanan { get; set; }
public string catatan { get; set; }
public string diagAwal { get; set; }
public string poliTujuan { get; set; }
public string klsRawat { get; set; }
public string lakaLantas { get; set; }
public string user { get; set; }
public string noMr { get; set; }
}
public class Data
{
public TSep t_sep { get; set; }
}
public class Request
{
public Data data { get; set; }
}
public class RootObject
{
public Request request { get; set; }
}
我设法在将对象转换为以下格式后成功发布数据:
<request>
<data>
<t_sep>
<noKartu>0000015223116</noKartu>
<tglSep>2016-06-1209:00:00 </tglSep>
<tglRujukan>2016 - 06 - 1209:00:00</tglRujukan>
<noRujukan>0204U0161016Y000155</noRujukan>
<ppkRujukan>00010002</ppkRujukan>
<ppkPelayanan>0301R001</ppkPelayanan>
<jnsPelayanan>2</jnsPelayanan>
<catatan>test</catatan>
<diagAwal>A00.1</diagAwal>
<poliTujuan>INT</poliTujuan>
<klsRawat>3</klsRawat>
<lakaLantas>2</lakaLantas>
<user>RS</user>
<noMr>RS002</noMr>
</t_sep>
</data>
</request>
使用此方法:
public string InsertSEP(string parameter)
{
var func = "SEP/insert";
var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
var signature = GetSignature(consumerId + "&" + unixTimestamp, secretKey);
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(BASE_URL);
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("X-cons-id", consumerId);
httpClient.DefaultRequestHeaders.Add("X-timestamp", unixTimestamp.ToString());
httpClient.DefaultRequestHeaders.Add("X-signature", signature);
var byteArray = Encoding.UTF8.GetBytes(parameter);
var byteContent = new ByteArrayContent(byteArray);
var full_url = string.Format("/{0}/{1}", Service_Name, func);
var response = httpClient.PostAsync(full_url, byteContent);
return response.Result.ToString();
}
}
但是当我尝试将对象序列化为json格式时,这样:
{
"request":{
"data":{
"t_sep":{
"noKartu":"0000015223116",
"tglSep":"2016-06-12 09:00:00",
"tglRujukan":"2016-06-12 09:00:00",
"noRujukan":"00001",
"ppkRujukan":"00010002",
"ppkPelayanan":"0301R001",
"jnsPelayanan":"2",
"catatan":"test",
"diagAwal":"A00.1",
"poliTujuan":"INT",
"klsRawat":"3",
"lakaLantas":"2",
"user":"RS",
"noMr":"RS002"
}
}
}
}
并使用这样的方法:
public string InsertSEP(string parameter)
{
var func = "SEP/insert";
var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
var signature = GetSignature(consumerId + "&" + unixTimestamp, secretKey);
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(BASE_URL);
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("X-cons-id", consumerId);
httpClient.DefaultRequestHeaders.Add("X-timestamp", unixTimestamp.ToString());
httpClient.DefaultRequestHeaders.Add("X-signature", signature);
var stringContent = new StringContent(parameter, Encoding.UTF8, "application/json");
var full_url = string.Format("/{0}/{1}", Service_Name, func);
var response = httpClient.PostAsync(full_url, stringContent);
return response.Result.ToString();
}
}
帖子失败了,第二个代码有什么问题,web服务器接受json作为参数,我在我的项目中使用Json.NET(Newtonsoft.Json),我更喜欢第二种方法,因为我不需要序列化反对自己,因为会有很多不同类型的对象要发布。
修改 我设法通过发送json字符串作为数据ByteArrayContent成功发送数据:
// parameter is in json format
var byteArray = Encoding.UTF8.GetBytes(parameter);
var byteContent = new ByteArrayContent(byteArray);
var full_url = string.Format("/{0}/{1}", Service_Name, func);
var response = httpClient.PostAsync(full_url, byteContent);
var responseBody = response.Result.Content.ReadAsStringAsync().Result;
但是这仍然没有回答我的问题,为什么我以StringContent