使用HttpWebRequest和参数的wcf服务消耗

时间:2015-01-22 07:17:47

标签: c# wcf httpwebrequest

wcf服务:

 [OperationContract]
            [WebInvoke(Method = "POST",
                          RequestFormat = WebMessageFormat.Json,
                          ResponseFormat = WebMessageFormat.Json,
                          BodyStyle = WebMessageBodyStyle.WrappedRequest
                          , UriTemplate = "/CreateReport/{value}"
                          )]
            string CreateReport(string value);

消耗服务:

string sURL = "http://localhost:2474/MyService.svc/CreateReport/asdasd";
            WebRequest wrGETURL;
            wrGETURL = WebRequest.Create(sURL);
            wrGETURL.Method = "POST";
            wrGETURL.ContentType = @"application/json; charset=utf-8";

        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] data = encoder.GetBytes("abcdef");
        wrGETURL.GetRequestStream().Write(data, 0, data.Length);

        HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse;

        Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
        StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
        string strResult = loResponseStream.ReadToEnd();
        loResponseStream.Close();
        webresponse.Close();
        Response.Write(strResult);

当我使用参数时,它会产生500内部服务器错误。没有参数,其工作正常。这段代码给了我很多麻烦,请帮帮我。

0 个答案:

没有答案