0

使用 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 方法会引发异常。注释中也提到了抛出异常的地方(在上面的代码中)。

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

额外资源

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

1 回答 1

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# 控制台应用程序)

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;
}
于 2013-11-11T21:05:57.470 回答