简体   繁体   中英

upload data to server using SOAP xml Webservice

hi i am using SOAP service to send data to server. here is my code :

 NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                        " <soap:Body>"
                        " <CreateNoteUsingXMl xmlns=\"http://www.Myurl.com\">"
                        " <AccountID>%@</AccountID>"
                         "<Username>%@</Username>"
                        " <Mypass>%@</Mypass>"
                        " <NoteXML>%@</NoteXML>"
                        " <isSign>0</isSign>"
                         "<noteID>%d</noteID>"
                         "</CreateNoteUsingXMl>"
                         "</soap:Body>"
                         "</soap:Envelope>",AccountNo,username,Mypass,xmlString,0];

here is a posting code :

NSURL *remoteURL = [NSURL URLWithString:@"http://ipad.myurl.com/PCNoteService.asmx"];

NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] initWithURL:remoteURL];
//A unique string that will be repeated as a separator
NSString *boundary = @"14737809831466499882746641449";

//this is important so the webserver knows that you're sending multipart/form-data
NSString *contentType = [NSString stringWithFormat:@"text/xml; boundary=%@", boundary];
[imageRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
[imageRequest addValue: @"http://www.myurl.com/CreateNoteUsingXMl" forHTTPHeaderField:@"SOAPAction"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; //boundary
 //[body appendData:[@"Content-Disposition: form-data; name=\"xmlString\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //your content header
[body appendData:[xmlString dataUsingEncoding:NSUTF8StringEncoding]]; //your content itself
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[imageRequest setHTTPMethod:@"POST"]; //set Method as POST
[imageRequest setHTTPBody:body];

i recieve a blank responce in connwecionDidfinish method it say "Received Bytes: 0" when i log the responce. i dont know why.

i think the function http://www.myurl.com/CreateNoteUsingXMl is not called. please help

我得到了解决方案。问题是我正在将xml字符串发送到参数中,但是服务器不接受“ <”和“>”标签,而当我将标签发送为“ <test>”时,它可以正常工作<to <和> to>自动(我不知道为什么要这么做,但是经过2天的凝灰岩测试后就知道了)。

You can use following example to complete your request :

//firstly create soap message string
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                         "<soap:Body>"
                         "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
                         "<Celsius>140.0</Celsius>"
                         "</CelsiusToFahrenheit>"
                         "</soap:Body>"
                         "</soap:Envelope>"];


//use url provide for requesting soap service
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//use SOAPAction which is provided in service
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection =  [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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