C# 使用datacontract使用WCF Restful服务
我创建了以下restfull web服务: 接口C# 使用datacontract使用WCF Restful服务,c#,wcf,rest,datacontract,C#,Wcf,Rest,Datacontract,我创建了以下restfull web服务: 接口 [ServiceContract] public interface ISIGService { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTem
[ServiceContract]
public interface ISIGService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetTicket/")]
Ticket GetTicket(string user, string pwd);
}
实施
public class SIGService : ISIGService
{
public Ticket GetTicket(string user, string pwd)
{
return new Ticket()
{
Usuario = "xx",
UsuarioNombre = "xxx",
UsuarioId = "xxx"
};
}
合同
[DataContract]
public class Ticket
{
[DataMember]
public int UsuarioId { get; set; }
[DataMember]
public string UsuarioNombre { get; set; }
[DataMember]
public string Usuario { get; set; }
}
我需要从web应用程序中使用此服务,并获取键入的对象票证
,我已为此提供了一个服务参考
服务器端代码:
string urlService =
String.Format("http://localhost:22343/SIGService.svc/GetTicket/?user='{0}'&pwd='{1}'",
usuario, password);
var request = (HttpWebRequest)WebRequest.Create(urlService);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
我放了一个text
变量只是为了得到一些东西,这里有点迷失了
我似乎没有得到这个对象,你能给我一些提示吗?很可能,你只需要将你的URL从
http://localhost:22343/SIGService.svc/GetTicket/?user='{0}'&pwd='{1}'
要使用正确的REST语法(因为您使用的是REST服务):
样本:
http://localhost:22343/SIGService.svc/GetTicket/daniel/topsecret
不需要?
或用户=
或单引号
这样,将{0}
中的值传递到用户
参数中,并将{1}
中的值传递到pwd
参数中
对于使用这项服务,我建议你去优秀的图书馆看看,它让你轻松使用REST服务
您的代码如下所示:
// set up the REST Client
string baseServiceUrl = "http://localhost:22343/SIGService.svc";
RestClient client = new RestClient(baseServiceUrl);
// define the request
RestRequest request = new RestRequest();
request.Method = Method.GET;
request.RequestFormat = DataFormat.Xml;
request.Resource = "GetTicket/{user}/{pwd}";
request.AddParameter("user", "daniel", ParameterType.UrlSegment);
request.AddParameter("pwd", "top$ecret", ParameterType.UrlSegment);
// make the call and have it deserialize the XML result into a Ticket object
var result = client.Execute<Ticket>(request);
if (result.StatusCode == HttpStatusCode.OK)
{
Ticket ticket = result.Data;
}
//设置REST客户端
字符串baseServiceUrl=”http://localhost:22343/SIGService.svc";
RestClient=新的RestClient(baseServiceUrl);
//定义请求
RestRequest请求=新的RestRequest();
request.Method=Method.GET;
request.RequestFormat=DataFormat.Xml;
request.Resource=“GetTicket/{user}/{pwd}”;
request.AddParameter(“user”、“daniel”、ParameterType.urlsecgment);
request.AddParameter(“pwd”,“top$ecret”,ParameterType.urlsem分段);
//进行调用并使其将XML结果反序列化为票证对象
var result=client.Execute(请求);
if(result.StatusCode==HttpStatusCode.OK)
{
票证=结果。数据;
}
您的服务器端配置是什么样子的???@marc_,谢谢您的响应,但是没有这个Rest库,您还有其他响应吗?Thanks@DanielV:好的,你可以像以前那样使用WebRequest
,但是你得到了一个XML字符串,你必须自己处理反序列化到一个对象中的问题-太多的工作了…代码工作得很完美,太多的工作使XML无法真正序列化。
// set up the REST Client
string baseServiceUrl = "http://localhost:22343/SIGService.svc";
RestClient client = new RestClient(baseServiceUrl);
// define the request
RestRequest request = new RestRequest();
request.Method = Method.GET;
request.RequestFormat = DataFormat.Xml;
request.Resource = "GetTicket/{user}/{pwd}";
request.AddParameter("user", "daniel", ParameterType.UrlSegment);
request.AddParameter("pwd", "top$ecret", ParameterType.UrlSegment);
// make the call and have it deserialize the XML result into a Ticket object
var result = client.Execute<Ticket>(request);
if (result.StatusCode == HttpStatusCode.OK)
{
Ticket ticket = result.Data;
}