C# 带有#char不';不要在WCF RESTful服务上工作
我正在用C#和.NET framework 4.0开发一个WCF RESTful服务 我有这个URI:C# 带有#char不';不要在WCF RESTful服务上工作,c#,wcf,rest,C#,Wcf,Rest,我正在用C#和.NET framework 4.0开发一个WCF RESTful服务 我有这个URI: http://localhost:7342/AdnLineService.svc/searchUsersByString?query=5%206%20#tags5&lookedAtBy=20 但是,在WCF代码中,在之后我没有得到任何东西 你知道为什么吗?似乎我不能在URI上使用字符。 我可以在URI上使用#吗?%23与#相同。请参见此 片段标识符(URI中#后面的部分)是客户端特性
http://localhost:7342/AdnLineService.svc/searchUsersByString?query=5%206%20#tags5&lookedAtBy=20
但是,在WCF代码中,在
之后我没有得到任何东西
你知道为什么吗?似乎我不能在URI上使用
字符。我可以在URI上使用
#
吗?%23与#相同。请参见此
片段标识符(URI中#后面的部分)是客户端特性。客户端检索资源而不将片段标识符传输到服务器,然后在资源中查找具有指定片段标识符的片段。为了澄清@dtb注释-并非严格意义上的客户端功能,但实际上大多数服务器会忽略它们,而不是通过浏览器传输。
public class Service
{
[WebInvoke(Method = "PUT", UriTemplate = "Graphs/{library}/{subjectLocalPart}/{predicateLocalPart}/{objectPart}/{languageCode}")]
public string CreateTriple(string library, string subjectLocalPart, string predicateLocalPart, string objectPart, string languageCode)
{
return string.Format("{0}-{1}-{2}-{3}-{4}", library, subjectLocalPart, predicateLocalPart, objectPart, languageCode);
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
Console.WriteLine("No '#'");
Util.SendRequest(baseAddress + "/Graphs/myLib/123abc/content-HasA/456def-ghik/en-us", "PUT", "application/json", "0");
Console.WriteLine("Simple '#' (encoded)");
Util.SendRequest(baseAddress + "/Graphs/myLib/123abc/content%23HasA/456def%23ghik/en-us", "PUT", "application/json", "0");
Console.WriteLine("Escaped '#'");
Util.SendRequest(baseAddress + "/Graphs/myLib/123abc/content%2523HasA/456def%2523ghik/en-us", "PUT", "application/json", "0");
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
public static class Util
{
public static string SendRequest(string uri, string method, string contentType, string body)
{
string responseBody = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Method = method;
if (!String.IsNullOrEmpty(contentType))
{
req.ContentType = contentType;
}
if (body != null)
{
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
req.GetRequestStream().Close();
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
resp = (HttpWebResponse)e.Response;
}
if (resp == null)
{
responseBody = null;
Console.WriteLine("Response is null");
}
else
{
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
foreach (string headerName in resp.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
}
Console.WriteLine();
Stream respStream = resp.GetResponseStream();
if (respStream != null)
{
responseBody = new StreamReader(respStream).ReadToEnd();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
}
}
Console.WriteLine();
Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
Console.WriteLine();
return responseBody;
}