簡體   English   中英

在C#中將JSON字符串發布到RESTful WCF服務時,返回400(錯誤請求)或空數據

[英]400 (Bad Request) or empty data when POSTing JSON string to RESTful WCF service in C#

我無法將數據發送到自托管的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 Bad Request”或GastObject為null。 我還嘗試將字符串聲明為參數,但這也為null。 請不要回答“使用搜索功能”。 除了搜索最近3天的答案外,我什么也沒有做;)歡迎任何提示! 提前致謝...

您嘗試通過調用“ 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 );
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM