400(错误请求)或在c#中向RESTful WCF服务发送JSON字符串时空数据

本文关键字:服务 JSON 字符串 数据 WCF RESTful 请求 错误 或在 中向 | 更新日期: 2023-09-27 18:01:25

我卡住了发送数据到一个自托管的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;
}
最后是data
[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天里,我除了寻找答案之外什么都不做;)欢迎任何提示!

400(错误请求)或在c#中向RESTful WCF服务发送JSON字符串时空数据

您尝试通过调用"SetGastImfo"来发送简单实例
实现是"SetGastInfo"
确定不是打错字吗?

答案既简单又难找:只发送JSON支持的数据!我试图发送一个名为位图的字节数组。但是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 );
    }
}