Ios 如何在objective c中将SOAP数据发布到服务器
我试图将数据发布到服务器,但它给了我错误 代码是Ios 如何在objective c中将SOAP数据发布到服务器,ios,objective-c,xib,Ios,Objective C,Xib,我试图将数据发布到服务器,但它给了我错误 代码是 -(IBAction)loginBtnClk:(id)sender { [self sendDataToServer :@"POST"]; } -(void) sendDataToServer : (NSString *) method{ int UserId=0; NSString *UserCode = userTf.text; int RoleId=12; NSString *Salutation
-(IBAction)loginBtnClk:(id)sender
{
[self sendDataToServer :@"POST"];
}
-(void) sendDataToServer : (NSString *) method{
int UserId=0;
NSString *UserCode = userTf.text;
int RoleId=12;
NSString *Salutation = @"Mr.";
NSString *FirstName=frstnmetf.text;
NSString *LastName =lstnametf.text;
NSString *Pwd=paswrdTf.text;
NSString *EmailId=@"abc@gmail.com";
NSString *Mobile=@"9876543210";
NSString *UserType=@"4";
int EmpId=0;
int ZoneId=575;
NSString *post = [NSString stringWithFormat:@"UserId=%d&UserCode=%@&RoleId=%d&Salutation=%@&FirstName=%@&LastName=%@&Pwd=%@EmailId=%@&Mobile=%@&UserType=%@&EmpId=%d&ZoneId=%d",UserId,UserCode,RoleId,Salutation,FirstName,LastName,Pwd,EmailId,Mobile,UserType,EmpId,ZoneId];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://URL?op=Insert_UserData"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"URL = %@",request);
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection link =%@",conn);
if(conn) {
mutableData =[[NSMutableData alloc] init];
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}
#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
[mutableData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
serverResponse.text = NO_CONNECTION;
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *myNSXMLParserfloorObj=[[NSXMLParser alloc]initWithData:mutableData];
myNSXMLParserfloorObj.delegate=self;
[myNSXMLParserfloorObj parse];
NSLog(@"%@",myNSXMLParserfloorObj.parserError);
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
//NSLog(@"Response from Server : %@", responseStringWithEncoded);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Insert_UserData"])
{
myDataClassObj=[[mydata alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
myMutableStringObj=[[NSMutableString alloc]initWithString:string];
NSLog(@"Array String: %@",myMutableStringObj);
NSData *data = [myMutableStringObj dataUsingEncoding:NSUTF8StringEncoding];
responsedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON DATA = %@",responsedict);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}
当我试图发布数据时,它会显示错误
2016-06-28 16:37:07.793 WebServiceDemo[5407:137315] Connection Successful
2016-06-28 16:37:13.035 WebServiceDemo[5407:137315] Array String: soap:Receiver
2016-06-28 16:37:13.035 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:15.241 WebServiceDemo[5407:137315] Array String: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---
2016-06-28 16:37:15.242 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:17.680 WebServiceDemo[5407:137315] Array String: >
2016-06-28 16:37:17.680 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:19.835 WebServiceDemo[5407:137315] Array String: System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
2016-06-28 16:37:19.835 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:21.555 WebServiceDemo[5407:137315] Array String:
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
2016-06-28 16:37:21.556 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:22.808 WebServiceDemo[5407:137315] Array String:
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
2016-06-28 16:37:22.808 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:23.810 WebServiceDemo[5407:137315] Array String:
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
2016-06-28 16:37:23.811 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:24.696 WebServiceDemo[5407:137315] Array String:
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
2016-06-28 16:37:24.696 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:25.532 WebServiceDemo[5407:137315] Array String:
at System.Xml.XmlReader.MoveToContent()
2016-06-28 16:37:25.532 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:26.200 WebServiceDemo[5407:137315] Array String:
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
2016-06-28 16:37:26.200 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:26.986 WebServiceDemo[5407:137315] Array String:
at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
2016-06-28 16:37:26.986 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:27.820 WebServiceDemo[5407:137315] Array String:
at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
2016-06-28 16:37:27.820 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:28.705 WebServiceDemo[5407:137315] Array String:
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
2016-06-28 16:37:28.705 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:29.474 WebServiceDemo[5407:137315] Array String:
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean
2016-06-28 16:37:29.475 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:30.309 WebServiceDemo[5407:137315] Array String: &
2016-06-28 16:37:30.309 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:31.129 WebServiceDemo[5407:137315] Array String: abortProcessing)
2016-06-28 16:37:31.130 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:31.881 WebServiceDemo[5407:137315] Array String:
--- End of inner exception stack trace ---
2016-06-28 16:37:31.882 WebServiceDemo[5407:137315] JSON DATA = (null)
2016-06-28 16:37:31.882 WebServiceDemo[5407:137315] (null)
我不明白问题是什么。正确地从服务器检索数据,但在发布时显示错误。
帮帮我 它终于运行了。只要我改变密码
-(IBAction)sendDataUsingGet:(id)sender{
[self sendDataToServer : @"GET"];
}
-(void) sendDataToServer : (NSString *) method{
int UserId=idtxt.text;
NSString *UserCode = userTf.text;
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>"
"<Method xmlns=\"http://tempuri.org/\">"
"<Paramener1>%d</Paramener1>"
"<Paramener2>%@</Paramener2>"
"</Method>"
"</soap:Body>"
"</soap:Envelope>",UserId,UserCode];
NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://URL/Method"]];
[request setHTTPMethod:@"POST"];
[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"URL = %@",request);
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection link =%@",conn);
if(conn) {
mutableData =[[NSMutableData alloc] init];
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}
#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
[mutableData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
serverResponse.text = NO_CONNECTION;
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *myNSXMLParserfloorObj=[[NSXMLParser alloc]initWithData:mutableData];
myNSXMLParserfloorObj.delegate=self;
[myNSXMLParserfloorObj parse];
NSLog(@"%@",myNSXMLParserfloorObj.parserError);
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
//NSLog(@"Response from Server : %@", responseStringWithEncoded);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Insert_UserData"])
{
myDataClassObj=[[mydata alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
myMutableStringObj=[[NSMutableString alloc]initWithString:string];
NSLog(@"Array String: %@",myMutableStringObj);
NSData *data = [myMutableStringObj dataUsingEncoding:NSUTF8StringEncoding];
responsedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON DATA = %@",responsedict);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}
-(iAction)sendDataUsingGet:(id)发送方{
[自发送数据到服务器:@“获取”];
}
-(void)sendDataToServer:(NSString*)方法{
int UserId=idtxt.text;
NSString*UserCode=userTf.text;
NSString*soapMessage=[NSString stringWithFormat:@]
""
""
""
%d
"%@"
""
""
“”,用户ID,用户代码];
NSData*postData=[soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString*postLength=[NSString stringWithFormat:@“%lu”,(无符号长)[soapMessage length]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置URL:[NSURL URLWithString:@”http://URL/Method"]];
[请求设置HttpMethod:@“POST”];
[请求addValue:@“text/xml;charset=utf-8”用于HttpHeaderField:@“内容类型”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
NSLog(@“URL=%@”,请求);
//[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:request委托:self];
NSLog(@“连接链接=%@”,conn);
如果(康涅狄格州){
mutableData=[[NSMutableData alloc]init];
NSLog(@“连接成功”);
}否则{
NSLog(@“无法建立连接”);
}
}
#pragma标记连接委托
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
[可变数据集长度:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[可变数据追加数据:数据];
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
serverResponse.text=没有连接;
返回;
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
NSXMLParser*mynsxmlparserfloorbj=[[NSXMLParser alloc]initWithData:mutableData];
mynsxmlparserfloorbj.delegate=self;
[mynsxmlparserfloorbj parse];
NSLog(@“%@”,mynsxmlparserfloorbj.parserror);
NSString*responseStringWithEncoded=[[NSString alloc]initWithData:mutableData编码:NSUTF8StringEncoding];
//NSLog(@“来自服务器的响应:%@”,responseStringWithEncoded);
NSAttributedString*attrStr=[[NSAttributedString alloc]initWithData:[responseStringWithEncoded数据使用编码:NSInocDestringEncoding]选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}文档属性:nil错误:nil];
serverResponse.attributedText=attrStr;
}
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName属性:(NSDictionary*)attributeDict
{
if([elementName IsequalString:@“插入用户数据”])
{
myDataClassObj=[[mydata alloc]init];
}
}
-(void)解析器:(NSXMLParser*)解析器查找字符:(NSString*)字符串
{
myMutableStringObj=[[NSMutableString alloc]initWithString:string];
NSLog(@“数组字符串:%@”,myMutableStringObj);
NSData*data=[myMutableStringObj数据使用编码:NSUTF8StringEncoding];
ResponseDect=[NSJSONSerialization JSONObjectWithData:数据选项:0错误:无];
NSLog(@“JSON数据=%@”,响应指令);
}
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName
{
}
出于调试目的,如果您可以在解析之前打印所有接收到的数据,这将非常有用。从解析的片段判断,web服务似乎期待一个不同的请求,即某个XML正文而不是URL编码值列表。然后,如何将URL编码值转换为XML正文。@“\n”“%@\n”“%@\n”“%@\n”“%@\n”“@Bhadresh我也尝试了同样的方法。但它显示了相同的错误。您需要有关有效请求的信息。我们无法向您提供此信息,因为这是特定于web服务的信息,我们不知道您使用的是什么web服务@BhadreshKathiriya的评论只是一个猜测,很可能是错误的。与负责web服务的人交谈。他们会知道的。什么是AFHTP要求操作??
-(IBAction)sendDataUsingGet:(id)sender{
[self sendDataToServer : @"GET"];
}
-(void) sendDataToServer : (NSString *) method{
int UserId=idtxt.text;
NSString *UserCode = userTf.text;
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>"
"<Method xmlns=\"http://tempuri.org/\">"
"<Paramener1>%d</Paramener1>"
"<Paramener2>%@</Paramener2>"
"</Method>"
"</soap:Body>"
"</soap:Envelope>",UserId,UserCode];
NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://URL/Method"]];
[request setHTTPMethod:@"POST"];
[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"URL = %@",request);
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection link =%@",conn);
if(conn) {
mutableData =[[NSMutableData alloc] init];
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}
#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
[mutableData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
serverResponse.text = NO_CONNECTION;
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *myNSXMLParserfloorObj=[[NSXMLParser alloc]initWithData:mutableData];
myNSXMLParserfloorObj.delegate=self;
[myNSXMLParserfloorObj parse];
NSLog(@"%@",myNSXMLParserfloorObj.parserError);
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
//NSLog(@"Response from Server : %@", responseStringWithEncoded);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Insert_UserData"])
{
myDataClassObj=[[mydata alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
myMutableStringObj=[[NSMutableString alloc]initWithString:string];
NSLog(@"Array String: %@",myMutableStringObj);
NSData *data = [myMutableStringObj dataUsingEncoding:NSUTF8StringEncoding];
responsedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON DATA = %@",responsedict);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}