【问题标题】:URI formats are not supported in webclient c#webclient c# 中不支持 URI 格式
【发布时间】:2017-12-12 13:47:04
【问题描述】:

如您所见,我调用我的服务:

  public ReceptionView ReceptionViewByReceptionId(string receptionId)
    {
        ClientRequest.Headers["Content-type"] = "application/json";
        ClientRequest.Encoding = System.Text.Encoding.UTF8;
        string result = ClientRequest.DownloadString("http://localhost:1111" + " /ReceptionService.svc/ReceptionViewByReceptionId/" + receptionId);

        var javascriptserializer = new JavaScriptSerializer();
        return javascriptserializer.Deserialize<ReceptionView>(result);
    }

但我在DownloadString 收到此错误:

{"URI formats are not supported."} 

我的服务工作正常,当我像这样通过浏览器调用我的服务时:

http://localhost:1111/ReceptionService.svc/ReceptionViewByReceptionId/1

我得到了预期的结果。但在我的应用程序中我不能

我的服务接口

 [WebGet( UriTemplate = "/ReceptionViewByReceptionId/{receptionId}", RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]

    ReceptionView ReceptionViewByReceptionId(string receptionId);

作为一个注释,我的函数之前没有任何错误,但今天突然返回这个错误。

我的提琴手在浏览器中的结果:

【问题讨论】:

  • 使用像wireshark或fiddler这样的嗅探器来学习一些东西。在浏览器中使用嗅探器并连接到您的服务并查看消息的格式。它不仅仅是 URL 和字符串。还有其他必须包含的标题信息。
  • @jdweng 我在 fiddler 中追踪了它,但服务 url 从未调用过为什么?
  • 结果为 200,表示已完成,因此连接成功完成。 cookie 可能已过期或与未完成的应用程序一起运行安装了错误的 cookie。通常,当我调试这些问题时,我建议在更改代码后从 IE 浏览器中删除 cookie,这样之前的不良结果就不会导致混淆。

标签: c# web-services wcf-rest webclient


【解决方案1】:

问题在于" /Reception 的空格,正如您在网址中看到的那样

string result = ClientRequest.DownloadString("http://localhost:1111" + " /ReceptionService.svc/ReceptionViewByReceptionId/" + receptionId);

我删除了它,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多