• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 objective-c 中将 SOAP 数据发布到服务器

[复制链接]
菜鸟教程小白 发表于 2022-12-13 17:13:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试将数据发布到服务器,但它给了我错误 代码是

-(IBAction)loginBtnClkid)sender
{
  [self sendDataToServer "OST"];
}

-(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=@"[email protected]";
    NSString *Mobile=@"9876543210";
    NSString *UserType=@"4";
    int EmpId=0;
    int ZoneId=575;

    NSString *post = [NSString stringWithFormat"UserId=%d&UserCode=%@&RoleId=%d&Salutation=%@&FirstName=%@&LastName=%@&wd=%@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"OST"];
    [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) connectionNSURLConnection *) connection didReceiveResponseNSURLResponse *)response
{
    [mutableData setLength:0];
}

-(void) connectionNSURLConnection *)connection didReceiveDataNSData *)data
{
    [mutableData appendData:data];
}

-(void) connectionNSURLConnection *)connection didFailWithErrorNSError *)error
{
    serverResponse.text = NO_CONNECTION;
    return;
}

-(void)connectionDidFinishLoadingNSURLConnection *)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)parserNSXMLParser *)parser didStartElementNSString *)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)

我不明白问题所在。数据已从服务器正确检索,但在发布时显示错误。 帮我。



Best Answer-推荐答案


它终于运行了。只是我改了代码

-(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/\">"
                             "<aramener1>%d</Paramener1>"
                             "<aramener2>%@</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:@"OST"];
    [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
{

}

关于ios - 如何在 objective-c 中将 SOAP 数据发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074657/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap