我正在尝试将图像文件从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>
有什么想法吗?谢谢!