使用SOAP使用WCF服务将图像从iPhone上传到服务器 [英] Upload image from iPhone to server using WCF service using SOAP

查看:255
本文介绍了使用SOAP使用WCF服务将图像从iPhone上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hiii,我正在为我的应用程序使用wcf SOAP服务,我发送如下所示的请求。

hiii, i'm using wcf SOAP services for my app and i send the request like below.

postStr = [NSString stringWithFormat:@"<?xml version=\"1.0\"?>\n"
           "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
           "<s:Body>\n"
           "<InsUpdDelActivityInfo xmlns=\"http://tempuri.org/\">\n"
           "<objEventsContent xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/iCampuslite.Model.ActivityStream\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
           "<d4p1:ActCommentId i:nil=\"true\" />\n"
           "<d4p1:ActSubTypeCd i:nil=\"true\" />\n"
           "<d4p1:ActType>Status</d4p1:ActType>\n"
           "<d4p1:ActTypeCd>1</d4p1:ActTypeCd>\n"
           "<d4p1:ActivityAnswersList />\n"
           "<d4p1:ActivityComments />\n"
           "<d4p1:ActivityId i:nil=\"true\" />\n"
           "<d4p1:ActivityLike />\n"
           "<d4p1:ActivityName>%@</d4p1:ActivityName>\n"
           "<d4p1:ActivityStreamImagesBytes>%@</d4p1:ActivityStreamImagesBytes>\n"
           "<d4p1:AnswerDesc i:nil=\"true\" />\n"
           "<d4p1:AnswerId>0</d4p1:AnswerId>\n"
           "<d4p1:CommentDesc i:nil=\"true\" />\n"
           "<d4p1:CommentId i:nil=\"true\" />\n"
           "<d4p1:CreatedUserId>%@</d4p1:CreatedUserId>\n"
           "<d4p1:CreatedUserName i:nil=\"true\" />\n"
           "<d4p1:FileOrLinkName i:nil=\"true\" />\n"
           "<d4p1:IsLiked>0</d4p1:IsLiked>\n"
           "<d4p1:IsTotalSchool i:nil=\"true\" />\n"
           "<d4p1:LikeCount>0</d4p1:LikeCount>\n"
           "<d4p1:LinkImage i:nil=\"true\" />\n"
           "<d4p1:ObjTypeCdId i:nil=\"true\" />\n"
           "<d4p1:ObjTypeId i:nil=\"true\" />\n"
           "<d4p1:OperationMode>I</d4p1:OperationMode>\n"
           "<d4p1:OperationType i:nil=\"true\" />\n"
           "<d4p1:OperationTypeId i:nil=\"true\" />\n"
           "<d4p1:OrganizationId>%@</d4p1:OrganizationId>\n"
           "<d4p1:OtherActivityId i:nil=\"true\" />\n"
           "%@\n"
           "</objEventsContent>\n"
           "<ismobile>true</ismobile>\n"
           "</InsUpdDelActivityInfo>\n"
           "</s:Body>\n"
           "</s:Envelope>", statusText, [appDelegateObj.loginUserInfoDict valueForKey:@"a:UserId"], [appDelegateObj.loginUserInfoDict valueForKey:@"a:OrgId"], workspaceStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@iCampusliteMobileService/ActivityStreamSl.svc", appDelegateObj.baseURL]]];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postStr length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://tempuri.org/IActivityStreamSl/InsUpdDelActivityInfo" forHTTPHeaderField:@"SOAPAction"];
[request addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

在上面的请求中有一个元素 ActivityStreamImagesBytes base64binary 参数我必须传递图像。

in the above request there is an element ActivityStreamImagesBytes which is base64binary parameter i have to pass the image.

我尝试过使用多种不同的格式。

I've tried using many different formats.

这里是wcf服务截图

here is wcf service screenshot

这里是服务器端代码

public string byteArrayToImage(byte[] byteArrayIn,string fileName)
    {
        if (byteArrayIn != null)
        {
            ActivityStreamSl.LogMsg("Byte Array Count : "+byteArrayIn.Length.ToString(), "D:\\log.txt");
            var serverfile = "D:somepath\somepath\somefolder";
            var getfile = HelperClass.Filesavehelper(Constants.UploadPaths.ActivityStream, "testfilename.png", serverfile);

            FileStream file = new FileStream(getfile, FileMode.Create);
            file.Write(byteArrayIn, 0, byteArrayIn.Length);
            file.Close();
            file.Dispose();
            return getfile;
        }
        else
        {
            ActivityStreamSl.LogMsg("Byte array is null","D:\\log.txt");
        }
        return "";
    }

服务器期待一个字节数组,我不知道如何发送它?
而且我不知道base64binary数据类型是什么?
我是否必须发送base64编码的字符串或字节数组或仅发送来自 NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@popular.png],0.7)的图像数据;

the server is expecting a byte array and i dont know how to send it? And i dont know what a base64binary data type is? Do I have to send the base64 encoded string or byte array or just data of image from NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"popular.png"], 0.7);

任何帮助都很明显

推荐答案

Hiii,
我有我的问题

Hiii, I've got what my problem is

在服务器端,它期望一个字节数组,

On server side, its expecting a byte array,

所以,我要按如下方式发送我的请求。

So, I've to send my request as follows.

将base64字符串转换为字节数组

Converting the base64 string into byte array

postStr = [NSString stringWithFormat:@"<?xml version=\"1.0\"?>\n"
       "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
       "<s:Body>\n"
       "<InsUpdDelActivityInfo xmlns=\"http://tempuri.org/\">\n"
       "<objEventsContent xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/iCampuslite.Model.ActivityStream\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
       "<d4p1:ActCommentId i:nil=\"true\" />\n"
       "<d4p1:ActSubTypeCd i:nil=\"true\" />\n"
       "<d4p1:ActType>Status</d4p1:ActType>\n"
       "<d4p1:ActTypeCd>1</d4p1:ActTypeCd>\n"
       "<d4p1:ActivityAnswersList />\n"
       "<d4p1:ActivityComments />\n"
       "<d4p1:ActivityId i:nil=\"true\" />\n"
       "<d4p1:ActivityLike />\n"
       "<d4p1:ActivityName>%@</d4p1:ActivityName>\n"
       "<d4p1:ActivityStreamImagesBytes dt: dt:"Base64Binary"><dt:length>%d</dt:length><dt:byte>%@</dt:byte>......................................</d4p1:ActivityStreamImagesBytes>\n"
       "<d4p1:AnswerDesc i:nil=\"true\" />\n"
       "<d4p1:AnswerId>0</d4p1:AnswerId>\n"
       "<d4p1:CommentDesc i:nil=\"true\" />\n"
       "<d4p1:CommentId i:nil=\"true\" />\n"
       "<d4p1:CreatedUserId>%@</d4p1:CreatedUserId>\n"
       "<d4p1:CreatedUserName i:nil=\"true\" />\n"
       "<d4p1:FileOrLinkName i:nil=\"true\" />\n"
       "<d4p1:IsLiked>0</d4p1:IsLiked>\n"
       "<d4p1:IsTotalSchool i:nil=\"true\" />\n"
       "<d4p1:LikeCount>0</d4p1:LikeCount>\n"
       "<d4p1:LinkImage i:nil=\"true\" />\n"
       "<d4p1:ObjTypeCdId i:nil=\"true\" />\n"
       "<d4p1:ObjTypeId i:nil=\"true\" />\n"
       "<d4p1:OperationMode>I</d4p1:OperationMode>\n"
       "<d4p1:OperationType i:nil=\"true\" />\n"
       "<d4p1:OperationTypeId i:nil=\"true\" />\n"
       "<d4p1:OrganizationId>%@</d4p1:OrganizationId>\n"
       "<d4p1:OtherActivityId i:nil=\"true\" />\n"
       "%@\n"
       "</objEventsContent>\n"
       "<ismobile>true</ismobile>\n"
       "</InsUpdDelActivityInfo>\n"
       "</s:Body>\n"
       "</s:Envelope>", statusText, [byteArray length], byteArray[0], byteArray[1], .................................., [appDelegateObj.loginUserInfoDict valueForKey:@"a:UserId"], [appDelegateObj.loginUserInfoDict valueForKey:@"a:OrgId"], workspaceStr];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat] :@%@ blah .... blah .... blah ....,appDelegateObj.baseURL]]];
NSString * messageLength = [NSString stringWithFormat:@%lu,(unsigned long)[postStr length]];
[request addValue:@text / xml; charset = utf -8forHTTPHeaderField:@Content-Type];
[request addValue:@http://tempuri.org/blah ..... .blah ..... blah ............forHTTPHeaderField:@SOAPAction];
[request addValue: messageLength forHTTPHeaderField:@Content-Length];
[request setHTTPMethod:@POST];
[request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
NSError * error = nil;
NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:& error];

I' ve托管了serv冰和我已经看到了请求的xml格式。谢谢你@ Duraiamuthan.H

I've hosted the services and there i've seen the xml format of the request. Any way thank you @Duraiamuthan.H

这篇关于使用SOAP使用WCF服务将图像从iPhone上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆