0

我正在尝试将图像文件从 iPad 发送到基于 WCF 的服务。我使用 JSON、Base64 作为图像数据。wcf 服务运行良好 - 我已经使用 Mike Gledhill 的优秀教程中的工具独立测试了它(使用我的应用程序生成的 JSON 字符串)。从我的 iPad 应用程序中,我可以毫无问题地发送/接收小的 JSON 字符串,但不能发送/接收带有图像的较大字符串(~1MB)。wcf 服务引发“未找到端点”错误。为什么这在我的 json 测试器中有效,但在 iOS 应用程序中无效?

iOS 代码:

+(int)writeJSONDataToURL:(NSString *)urlString dictData:(NSDictionary *)dictData
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictData options:NSJSONWritingPrettyPrinted error:&error];

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    NSLog(@"JSON Output: %@", jsonString);

    NSString *postLength = [NSString stringWithFormat:@"%d", [jsonString length]];

    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:jsonData];

    NSURLResponse *response;
    NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
    NSLog(@"Reply from web layer: %@", theReply);
    return 0;
}

和 wcf 服务:

       [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "addImage")]
    int AddImage(Stream JSONdataStream);

       public int AddImage(Stream JSONdataStream)
    {
        int result;
        try
        {
            // Read in our Stream into a string...
            StreamReader reader = new StreamReader(JSONdataStream);
            string JSONdata = reader.ReadToEnd();

            // ..then convert the string into a single "Image" record.
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Image i = jss.Deserialize<Image>(JSONdata);
            if (i == null)
            {
                // Error: Couldn't deserialize our JSON string into an "Image" object.
                result = 0;
                return result;
            }

            byte[] imgBytes = Convert.FromBase64String(i.LocalPath);
            ELDataContext dc = new ELDataContext();
            Image newImage = new Image()
            {
                // imageid is autoincrement
                QuoteID = i.QuoteID,
                ImageDate = i.ImageDate,
                ImageNotes = i.ImageNotes,
                Thumbnail = imgBytes
                // ignore localpath
                // ignore localimageid
            };

            dc.Images.InsertOnSubmit(newImage);
            dc.SubmitChanges();

            result = 1;
            return result;
        }

服务的 Web.Config:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="ELConnectionString" connectionString="***"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="ELWS.Service1" behaviorConfiguration="ELWS.Service1Behavior">
        <endpoint address="../Service1.svc"
            binding="webHttpBinding"
            bindingConfiguration="basicBinding"
            contract="ELWS.IService1"
            behaviorConfiguration="webBehaviour" />
      </service>
    </services>   
    <bindings>
      <webHttpBinding>        
        <binding name="basicBinding" 
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647" 
                 maxBufferSize="2147483647"
                 openTimeout="00:10:00" 
                 sendTimeout="00:10:00"
                 >
          <readerQuotas maxDepth="2147483647"
                  maxStringContentLength="2147483647"
                  maxArrayLength="2147483647"
                  maxBytesPerRead="2147483647"
                  maxNameTableCharCount="2147483647"
                  />
          <security mode="None" />
        </binding>
      </webHttpBinding>      
    </bindings>   
    <behaviors>
      <serviceBehaviors>
        <behavior name="ELWS.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>      
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>

有任何想法吗?谢谢!

4

0 回答 0