使用webrequest getmethod消耗wcf服务给出404错误

时间:2012-04-11 21:54:09

标签: asp.net wcf c#-4.0 httpwebrequest wcf-binding

我使用Http WebRequest调用我的WCF WebService。它的工作正常,但当我在参数中传递一些特殊字符时,它会产生错误404.找不到页面..

这是我的代码片段。

[OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "Data/{username}/{password}")]
        string RequestData(string userName, string password);
    public string RequestData(string userName, string password)
        {
             // Here is My Logic Comes

         }

// Here im Calling This Service in My ASPX Page 

HttpWebRequest req = null;
            HttpWebResponse res = null;
            try
            {
                const string url = "http://localhost:53366/AuthService.svc/Data/test@test.com/password#1"; // Here Gives Error 404 Page Not Found

//const string url = "http://localhost:53366/AuthService.svc/Data/test/password"; // Here Works Fine When I Pass This Paramters Without Special Characters
                req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "Get";
                req.ContentType = "application/json; charset=utf-8";

                req.Headers.Add("AppID", "MobileApplication");

                res = (HttpWebResponse)req.GetResponse();
                Stream responseStream = res.GetResponseStream();
                var streamReader = new StreamReader(responseStream);

                txtTokenRespoonse.Text = streamReader.ReadToEnd();

                streamReader.Close();
                streamReader.Dispose();

                responseStream.Close();
                responseStream.Dispose();
            }

1 个答案:

答案 0 :(得分:0)

如果要将任何特殊字符传递给URL,则必须对其进行编码!

尝试:

const string url = "http://localhost:53366/AuthService.svc/Data/test%40test.com/password%231";

顺便说一下。在URL中传递密码只是一个样本,不是吗?