我正在尝试使用 REST 发送到公开的 Web 服务。但我得到的是 400(错误请求。)
您如何通过 REST 发送模型,该模型将正确映射到另一端的模型。我不想做的是将所有 100 个参数单独暴露在连接点中。
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "SaveData",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void SaveData(datum data);
但我无法做的是传递一个有效的基准对象。
// Build JSON string
JSONStringer vehicle;
try {
vehicle = new JSONStringer()
.object()
.key("datum")
.object()
.key("ID").value(5)
.key("Name").value("test")
.key("No").value(54)
.key("Description").value("Test")
.endObject()
.endObject();
HttpPost request = new HttpPost(address + "/SaveData");
Log.d("WebInvoke", "Connection : " + address + "/SaveData");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(vehicle.toString());
Log.d("StringEntity", vehicle.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
// Saving : 400
基准类:
[DataContract]
public partial class datum
{
[DataMember(Name = "ID")]
public int ID { get; set; }
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "No")]
public Nullable<int> No { get; set; }
[DataMember(Name = "Description")]
public string Description { get; set; }
}