【问题标题】:400 (Bad Request) or empty data when POSTing JSON string to RESTful WCF service in C#在 C# 中将 JSON 字符串发布到 RESTful WCF 服务时出现 400(错误请求)或空数据
【发布时间】: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


    【解决方案1】:

    您尝试通过调用“SetGastImfo”发送简单实例
    实现是“SetGastInfo”
    确定不是错字?

    【讨论】:

    • 是的,这是一个错字 - 但它在服务的两侧。刚刚纠正了错字,但这不是 400 的原因;)
    【解决方案2】:

    答案很简单,但很难找到: 只发送 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 );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多