我无法将数据发送到自托管的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 );
这取决于我尝试更改我得到的一些参数&#34; 400 Bad Request&#34;或者GastObject为空。 我还尝试将字符串声明为参数,但这也是null。 请不要回答&#34;使用搜索功能&#34;。除了在过去3天内搜索答案,我什么都不做;) 任何提示都是受欢迎的! 提前谢谢......
答案 0 :(得分:0)
您尝试通过调用&#34; SetGastImfo&#34;发送简单实例
实现是&#34; SetGastInfo&#34;
当然不是拼写错误?
答案 1 :(得分:0)
答案很简单,很难找到: 仅发送JSON支持的数据! 我试图发送一个名为bitmap的字节数组。但是JSON不支持字节数组。 如果要发送字节数组,则需要将其转换为具有BASE64编码的字符串。所以不要将位图装饰为DataMember,而是使用此解决方案:
private Byte[] bitmap = null;
public Byte[] Bitmap {
get { return bitmap; }
set { bitmap = value; }
}
[DataMember]
public string BitmapBASE64 {
get {
return Convert.ToBase64String( bitmap );
}
set {
bitmap = Convert.FromBase64String( value );
}
}