【问题标题】:WCF REstful Service Passing user and password on UriWCF REstful 服务在 Uri 上传递用户和密码
【发布时间】:2016-08-17 14:45:54
【问题描述】:

我确实有一个 WCF RESTful .NET 4.0 服务,它运行并使用来自 URI 正文的值 #Json。

我调用我的方法的方式如下:

使用海报,作为 POST 请求 http://localhost:25512/JSONService1.svc/CreateItem

主体:

= YOLO

工作正常!

但我也想使用 uri,

http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111

这是我的代码

这是在 Service1.cs 上

[OperationContract]
[WebInvoke(UriTemplate = "CreateItem",  // ISSUES <<< not sure what to do here
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, Method = "POST")]    

void CreateItem(Stream streamOfData,string uid, string pass);

还有与之相关的代码,Service1.svc.cs

public void CreateItem(Stream streamOfData, string uid, string pass)
{
        StreamReader reader = new StreamReader(streamOfData);
        String res = reader.ReadToEnd();
        NameValueCollection coll = HttpUtility.ParseQueryString(res);
}

【问题讨论】:

  • “我也想使用 uri”是什么意思?
  • 也许我应该叫它 URL 而不是 URI ,我想传递/使用“uid and pass”参数并在 CreateItem 方法中使用它们
  • 好的,所以你想知道客户端发送请求时实际的 URI 是什么?
  • 设备“客户端”将始终发送以“?uid=aaa&pass=111”结尾的请求,包括正文中的 JSON。但是,我到目前为止的项目只接受身体。
  • 知道了。所以我的问题是,你想知道完整的 URI 是什么吗?

标签: c# json rest wcf


【解决方案1】:

要获取原始 URI,您可以执行以下操作:

string originalUri = System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;

您可以使用System.Uri 类和/或HttpUtility 类来解析Uri。

要使用 System.Uri 类,只需传入 Uri。

Uri myUri = new Uri("http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111");

或根据您的要求:

Uri myUri = new Uri(originalUri);

然后您可以使用一些内置属性检查 Uri 的各个部分。

【讨论】:

  • 感谢 =D 帮助
猜你喜欢
  • 2013-11-23
  • 1970-01-01
  • 2012-07-17
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
相关资源
最近更新 更多