使用JSON时具有PUT操作的Restful WCF(服务器错误400)

时间:2013-11-07 06:36:37

标签: wcf put wcf-rest internal-server-error

使用webHTTPBinding创建了Restful WCF服务

在我的客户端应用程序中使用该服务时,遇到此错误

远程服务器返回错误:(400)错误请求。(已经尝试过设置maxReceivedMessageSize和其他在线提及的解决方案)

场景:

客户端的2种方法

1)工作正常 * * GET请求**

private static void GenerateGETRequest()
{
            HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
            GETRequest.Method = "GET";
            GETRequest.ContentType = "application/json"; 

            Console.WriteLine("Sending GET Request");

            HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
            Stream GETResponseStream = GETResponse.GetResponseStream();
            StreamReader sr = new StreamReader(GETResponseStream);

            Console.WriteLine("Response from Restful Service");
            Console.WriteLine(sr.ReadToEnd());
}

2)异常 * ** * ** (带响应的PUT请求)**

private static void GeneratePUTRequest()
{

            byte[] dataByte = CreateJSONObject(Object); //this custom method converts object that I pass to JSON serialized object

            HttpWebRequest PUTRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
            PUTRequest.Method = "PUT";
            **//PUTRequest.ContentType = "application/json"; //error 400 when un-commenting this**
            PUTRequest.ContentLength = dataByte.Length;

            Stream PUTRequestStream = PUTRequest.GetRequestStream();
            PUTRequestStream.Write(dataByte, 0, dataByte.Length);

            **HttpWebResponse PUTResponse = (HttpWebResponse)PUTRequest.GetResponse(); // this is where i get the exception when un-commenting above line**

            Stream PUTResponseStream = PUTResponse.GetResponseStream();
            StreamReader sr = new StreamReader(PUTResponseStream);

            Console.WriteLine("Response from Restful Service");
            Console.WriteLine(sr.ReadToEnd());
}
当我取消评论注释中提到的行(在代码中)时,

2方法抛出xception。注释中也提到了抛出异常的地方(在上面的代码中)。

第二种方法适用于所需的输出(如果我评论上述行)。

其他资源

[OperationContract]
[WebInvoke(Method = "PUT",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "Controller")]

1 个答案:

答案 0 :(得分:0)

要通过POST或PUT发送数据,您需要根据WCF服务正确构建数据。这基本上就是你需要的(只需将POST更改为你的应用程序的PUT)

1)WCF服务接口

[OperationContract]
[WebInvoke(Method = "POST",
    UriTemplate = "GetData",
    RequestFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(DataRequest parameter);

2)WCF服务实施

public string GetData(DataRequest parameter)
{
    //Do stuff
    return "your data here";
}

3)WCF服务中的数据合同(在本例中为DataRequest)

[DataContract(Namespace = "YourNamespaceHere")]
public class DataRequest
{
    [DataMember]
    public string ID{ get; set; }
    [DataMember]
    public string Data{ get; set; }
}

4)发送数据的客户必须正确构建数据! (在这种情况下是C#console app)

static void Main(string[] args)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    string SampleXml = "<DataRequest xmlns=\"YourNamespaceHere\">" +
                                    "<ID>" +
                                    yourIDVariable +
                                    "</ID>" +
                                    "<Data>" +
                                    yourDataVariable +
                                    "</Data>" +
                                "</DataRequest>";

    string postData = SampleXml.ToString();
    byte[] data = encoding.GetBytes(postData);

    string url = "http://localhost:62810/MyService.svc/GetData";

    string strResult = string.Empty;

    // declare httpwebrequet wrt url defined above
    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
    // set method as post
    webrequest.Method = "POST";
    // set content type
    webrequest.ContentType = "application/xml";
    // set content length
    webrequest.ContentLength = data.Length;
    // get stream data out of webrequest object
    Stream newStream = webrequest.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    //Gets the response
    WebResponse response = webrequest.GetResponse();
    //Writes the Response
    Stream responseStream = response.GetResponseStream();

    StreamReader sr = new StreamReader(responseStream);
    string s = sr.ReadToEnd();

    return s;
}