0

我用 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();
            }
4

1 回答 1

0

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

尝试:

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

顺便提一句。在 URL 中传递密码只是一个示例,不是吗?

于 2012-04-12T10:17:46.950 回答