【发布时间】:2015-07-06 15:39:56
【问题描述】:
我在向自托管 Web 服务发送数据时遇到了困难。 到目前为止我得到了什么:
服务合同和实施(顺便说一句。GET 就像一个魅力)
[WebGet( UriTemplate = "/gast/{customer_id}/{year}/{gast_id}", ResponseFormat = WebMessageFormat.Json )]
[OperationContract]
public acsEintrittGast GetGastInfo( string customer_id, string year, string gast_id ) {
acsEintrittGast gast = new acsEintrittGast();
gast.ID = Guid.Parse( gast_id );
gast.Bitmap = File.ReadAllBytes( dataPath + customer_id + "\\" + year + "\\" + gast_id + ".jpg" );
return gast;
}
[WebInvoke( UriTemplate = "/gast/{customer_id}/{year}",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
Method = "POST" )]
[OperationContract]
public acsEintrittGast SetGastInfo( string customer_id, string year, acsEintrittGast GastObject /*string GastString*/ ) {
acsEintrittGast Gast = null;
try {
//Gast = JsonConvert.DeserializeObject<acsEintrittGast>( (string)GastObject);
File.WriteAllBytes( dataPath + customer_id + "\\" + year + "\\" + Gast.ID.ToString() + ".jpg", Gast.Bitmap.ToArray<byte>() );
}
catch {
}
return Gast;
}
服务消费者方法(在windows phone 8.1上运行)
public async static Task<T> SetRESTData( string URI, T Content ) {
T res = default( T );
HttpClient httpClient = null;
HttpResponseMessage response = null;
StreamReader sr = null;
StreamWriter writer = null;
MemoryStream ms = null;
try {
httpClient = new HttpClient();
string s = JsonConvert.SerializeObject( Content );
StringContent theContent = new StringContent( s, System.Text.Encoding.UTF8, "application/json" );
using( HttpRequestMessage request = new HttpRequestMessage( HttpMethod.Post, BaseURI + URI ) ) {
request.Content = theContent;
response = await httpClient.SendAsync( request );
}
}
catch {
}
finally {
if( sr != null ) {
sr.Close();
sr = null;
}
if( writer != null ) {
writer.Close();
writer = null;
}
if( ms != null ) {
ms.Dispose();
ms = null;
}
if( response != null ) {
response.Dispose();
response = null;
}
if( httpClient != null ) {
httpClient.Dispose();
httpClient = null;
}
}
return res;
}
最后是数据
[DataContract( Name = "acsEintrittGast", Namespace = "" )]
public class acsEintrittGast {
private Guid id = Guid.NewGuid();
[DataMember( Name = "id", Order = 1 )]
public Guid ID {
get { return id; }
set { id = value; }
}
private Byte[] bitmap = null;
[DataMember( Name = "bitmap", Order = 1 )]
public Byte[] Bitmap {
get { return bitmap; }
set { bitmap = value; }
}
private DateTime lastEntry = new DateTime( 1900, 1, 1 );
[DataMember( Name = "lastEntry", Order = 1 )]
public DateTime LastEntry {
get { return lastEntry; }
set { lastEntry = value; }
}
}
当我尝试发送一个简单的实例时
acsEintrittGast gast = new acsEintrittGast();
gast.Bitmap = new byte[80455];
gast.ID = Guid.NewGuid();
await acsService.SetGastInfo( ftpLicID, currentYear, gast );
这取决于我尝试更改某些参数我得到“400 错误请求”或 GastObject 为空。 我也尝试将字符串声明为参数,但这也是空的。 请不要回答“使用搜索功能”。我除了在过去 3 天里寻找答案之外什么也没做;) 欢迎任何提示! 提前谢谢...
【问题讨论】:
标签: c# json wcf rest bad-request