繁体   English   中英

相当于PHP文件的WCF C#.NET在Image上获取内容以用于iOS

[英]WCF C# .NET equivalent of PHP file get contents on Image for use with iOS

我目前有一个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>

要返回数据流,您需要将数据作为Stream而不是string 如果您以字符串形式返回数据,则该数据将以该字符串的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM