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 に答える