c# - WCF C# .NET 相当于 PHP 文件获取图像上的内容以与 iOS 一起使用

标签 c# .net ios wcf

我目前有一个 PHP 脚本,它给出了服务器上图像的路径,将图像转换为数据流并回显该字符串。

<?php
$path = htmlspecialchars{$_POST['URL']);
    $img = file_get_contents($path);
    echo $img;
?>

然后,我使用 PHP 的响应将数据流转换为 iOS 中的图像,如下所示:

NSData *pictureData = [data getPicture:currentPerson.picture_URL];
personPicture = [UIImage imageWithData:pictureData];

如何在 C# 中完成相同的任务,而无需在 iOS 端更改太多代码。目前我知道如何使用用 .NET C# 编写的 WCF 服务返回 JSON 对象。但是,我无法弄清楚如何像在 PHP 中那样返回数据流。

还有什么方法可以让这个过程更安全吗?

--编辑--

当前的实现不起作用:

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "/picture/{path}")]
string GetPictureMethod(string path);

public string GetPictureMethod(string path)
{
   path = "(test path here)";
   string picData = System.IO.File.ReadAllText(path);
   return picData;
}

执行 `NSLog(@"Data: %@", data) 给出以下结果:

Picture data: <efbbbf3c 3f786d6c 20766572 73696f6e 3d22312e 30222065 6e636f64 696e673d 22757466 2d38223f 3e0d0a3c 21444f43 54595045 2068746d 6c205055 424c4943 20222d2f 2f573343 2f2f4454 44205848 544d4c20 312e3020 5472616e 73697469 6f6e616c 2f2f454e 22202268 7474703a 2f2f7777 772e7733 2e6f7267 2f54522f 7868746d 6c312f44 54442f78 68746d6c 312d7472 616e7369 74696f6e 616c2e64 7464223e 0d0a3c68 746d6c20 786d6c6e 733d2268 7474703a 2f2f7777 772e7733 2e6f7267 2f313939 392f7868 746d6c22 3e0d0a20 203c6865 61643e0d 0a202020 203c7469 746c653e 53657276 6963653c 2f746974 6c653e0d 0a202020 203c7374 796c653e 424f4459 207b2063 6f6c6f72 3a202330 30303030 303b2062 61636b67 726f756e 642d636f 6c6f723a 20776869 74653b20 666f6e74 2d66616d 696c793a 20566572 64616e61 3b206d61 7267696e 2d6c6566 743a2030 70783b20 6d617267 696e2d74 6f703a20 3070783b 207d2023 636f6e74 656e7420 7b206d61 7267696e 2d6c6566 743a2033 3070783b 20666f6e 742d7369 7a653a20 2e373065 6d3b2070 61646469 6e672d62 6f74746f 6d3a2032 656d3b20 7d20413a 6c696e6b 207b2063 6f6c6f72 3a202333 33363639 393b2066 6f6e742d 77656967 68743a20 626f6c64 3b207465 78742d64 65636f72 6174696f 6e3a2075 6e646572 6c696e65 3b207d20 413a7669 73697465 64207b20 636f6c6f 723a2023 36363939 63633b20 666f6e74 2d776569 6768743a 20626f6c 643b2074 6578742d 6465636f 72617469 6f6e3a20 756e6465 726c696e 653b207d 20413a61 63746976 65207b20 636f6c6f 723a2023 33333636 39393b20 666f6e74 2d776569 6768743a 20626f6c 643b2074 6578742d 6465636f 72617469 6f6e3a20 756e6465 726c696e 653b207d 202e6865 6164696e 6731207b 20626163 6b67726f 756e642d 636f6c6f 723a2023 30303333 36363b20 626f7264 65722d62 6f74746f 6d3a2023 33333636 39392036 70782073 6f6c6964 3b20636f 6c6f723a 20236666 66666666 3b20666f 6e742d66 616d696c 793a2054 61686f6d 613b2066 6f6e742d 73697a65 3a203236 70783b20 666f6e74 2d776569 6768743a 206e6f72 6d616c3b 6d617267 696e3a20 30656d20 30656d20 31307078 202d3230 70783b20 70616464 696e672d 626f7474 6f6d3a20 3870783b 20706164 64696e67 2d6c6566 743a2033 3070783b 70616464 696e672d 746f703a 20313670 783b7d20 70726520 7b20666f 6e742d73 697a653a 736d616c 6c3b2062 61636b67 726f756e 642d636f 6c6f723a 20236535 65356363 3b207061 6464696e 673a2035 70783b20 666f6e74 2d66616d 696c793a 20436f75 72696572 204e6577 3b206d61 7267696e 2d746f70 3a203070 783b2062 6f726465 723a2031 70782023 66306630 65302073 6f6c6964 3b207768 6974652d 73706163 653a2070 72652d77 7261703b 20776869 74652d73 70616365 3a202d70 72652d77 7261703b 20776f72 642d7772 61703a20 62726561 6b2d776f 72643b20 7d207461 626c6520 7b20626f 72646572 2d636f6c 6c617073 653a2063 6f6c6c61 7073653b 20626f72 6465722d 73706163 696e673a 20307078 3b20666f 6e742d66 616d696c 793a2056 65726461 6e613b7d 20746162 6c652074 68207b20 626f7264 65722d72 69676874 3a203270 78207768 69746520 736f6c69 643b2062 6f726465 722d626f 74746f6d 3a203270 78207768 69746520 736f6c69 643b2066 6f6e742d 77656967 68743a20 626f6c64 3b206261 636b6772 6f756e64 2d636f6c 6f723a20 23636563 6639633b 7d207461 626c6520 7464207b 20626f72 6465722d 72696768 743a2032 70782077 68697465 20736f6c 69643b20 626f7264 65722d62 6f74746f 6d3a2032 70782077 68697465 20736f6c 69643b20 6261636b 67726f75 6e642d63 6f6c6f72 3a202365 35653563 633b7d3c 2f737479 6c653e0d 0a20203c 2f686561 643e0d0a 20203c62 6f64793e 0d0a2020 20203c64 69762069 643d2263 6f6e7465 6e74223e 0d0a2020 20202020 3c702063 6c617373 3d226865 6164696e 6731223e 53657276 6963653c 2f703e0d 0a202020 2020203c 703e4d65 74686f64 206e6f74 20616c6c 6f776564 2e3c2f70 3e0d0a20 2020203c 2f646976 3e0d0a20 203c2f62 6f64793e 0d0a3c2f 68746d6c 3e>

最佳答案

要返回数据流,您需要将数据作为返回,而不是作为字符串。如果您将数据作为字符串返回,它将作为该字符串的 JSON 编码表示形式返回。

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "/picture/{path}")]
System.IO.Stream GetPictureMethod(string path);

public System.IO.Stream GetPictureMethod(string path)
{
    path = "(test path here)";
    byte[] picData = System.IO.File.ReadAllBytes(path);
    WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; // or the correct type
    return new System.IO.MemoryStream(picData);
}

顺便说一句,您应该使用[WebGet]而不是[WebInvoke(Method = "GET")]

更多数据:您拥有的 NSData 相当于下面的代码。您应该首先尝试使用浏览器或Fiddler等工具访问WCF服务,检查是否取回数据。只有这样你才应该尝试从 NSData 中使用它。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p>Method not allowed.</p>
    </div>
  </body>
</html>

关于c# - WCF C# .NET 相当于 PHP 文件获取图像上的内容以与 iOS 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14322869/

相关文章:

c# - .net 中引发事件的死锁

objective-c - ios应用程序视频效果问题

ios - 如何为整个应用程序制作一个全局计时器

ios - 如何在具有两个不同单元格的 TableView 中添加一行

c# - PropertieInfo 的 GetValue 抛出 TargetParameterCountException (System.Reflection)

c# - 将焦点设置到 Xamarin.Forms 中的条目

c# - F# 按多个值进行分组并聚合

c# - 三层应用程序中 EF Core 中的自动迁移

c# - 如何在WPF中轻松绘制图形?

c# - 获得焦点和失去焦点这两个事件在处理多个文本框的获得和失去焦点事件时重复调用它进入无限循环