菜鸟教程小白 发表于 2022-12-13 17:13:09

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


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

<pre><code>-(IBAction)loginBtnClk:(id)sender
{
;
}

-(void) sendDataToServer : (NSString *) method{

    int UserId=0;
    NSString *UserCode= userTf.text;
    int RoleId=12;
    NSString *Salutation = @&#34;Mr.&#34;;
    NSString *FirstName=frstnmetf.text;
    NSString *LastName =lstnametf.text;
    NSString *Pwd=paswrdTf.text;
    NSString *EmailId=@&#34;[email protected]&#34;;
    NSString *Mobile=@&#34;9876543210&#34;;
    NSString *UserType=@&#34;4&#34;;
    int EmpId=0;
    int ZoneId=575;

    NSString *post = ;
    NSData *postData = ;

    NSString *postLength = ];
    NSMutableURLRequest *request = [ init];
    ];
    ;
    ;
    NSLog(@&#34;URL = %@&#34;,request);
    ;
    ;
    NSURLConnection *conn = [ initWithRequest:request delegate:self];
    NSLog(@&#34;Connection link =%@&#34;,conn);
    if(conn) {
      mutableData =[ init];
      NSLog(@&#34;Connection Successful&#34;);
    } else {
      NSLog(@&#34;Connection could not be made&#34;);
    }

}

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    ;
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    ;
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    serverResponse.text = NO_CONNECTION;
    return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSXMLParser *myNSXMLParserfloorObj=[initWithData:mutableData];
    myNSXMLParserfloorObj.delegate=self;
    ;
    NSLog(@&#34;%@&#34;,myNSXMLParserfloorObj.parserError);
    NSString *responseStringWithEncoded = [ initWithData: mutableData encoding:NSUTF8StringEncoding];
    //NSLog(@&#34;Response from Server : %@&#34;, responseStringWithEncoded);
    NSAttributedString * attrStr = [ initWithData: 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()
    {
      myDataClassObj=[init];
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    myMutableStringObj=[initWithString:string];
    NSLog(@&#34;Array String: %@&#34;,myMutableStringObj);
    NSData *data = ;
    responsedict = ;
    NSLog(@&#34;JSON DATA = %@&#34;,responsedict);


}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

}
</code></pre>

<p>当我尝试发布数据时,它显示错误</p>

<pre><code>2016-06-28 16:37:07.793 WebServiceDemo Connection Successful
2016-06-28 16:37:13.035 WebServiceDemo Array String: soap:Receiver
2016-06-28 16:37:13.035 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:15.241 WebServiceDemo Array String: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---
2016-06-28 16:37:15.242 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:17.680 WebServiceDemo Array String: &gt;
2016-06-28 16:37:17.680 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:19.835 WebServiceDemo Array String:System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
2016-06-28 16:37:19.835 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:21.555 WebServiceDemo Array String:
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
2016-06-28 16:37:21.556 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:22.808 WebServiceDemo Array String:
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
2016-06-28 16:37:22.808 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:23.810 WebServiceDemo Array String:
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
2016-06-28 16:37:23.811 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:24.696 WebServiceDemo Array String:
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
2016-06-28 16:37:24.696 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:25.532 WebServiceDemo Array String:
   at System.Xml.XmlReader.MoveToContent()
2016-06-28 16:37:25.532 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:26.200 WebServiceDemo Array String:
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
2016-06-28 16:37:26.200 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:26.986 WebServiceDemo Array String:
   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
2016-06-28 16:37:26.986 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:27.820 WebServiceDemo Array String:
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
2016-06-28 16:37:27.820 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:28.705 WebServiceDemo Array String:
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
2016-06-28 16:37:28.705 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:29.474 WebServiceDemo 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 JSON DATA = (null)
2016-06-28 16:37:30.309 WebServiceDemo Array String: &amp;
2016-06-28 16:37:30.309 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:31.129 WebServiceDemo Array String:abortProcessing)
2016-06-28 16:37:31.130 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:31.881 WebServiceDemo Array String:
   --- End of inner exception stack trace ---
2016-06-28 16:37:31.882 WebServiceDemo JSON DATA = (null)
2016-06-28 16:37:31.882 WebServiceDemo (null)
</code></pre>

<p>我不明白问题所在。数据已从服务器正确检索,但在发布时显示错误。
帮我。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>它终于运行了。只是我改了代码</p>

<pre><code>-(IBAction)sendDataUsingGet:(id)sender{

    ;
}
-(void) sendDataToServer : (NSString *) method{

    int UserId=idtxt.text;
    NSString *UserCode= userTf.text;

    NSString *soapMessage = [NSString stringWithFormat:@&#34;&lt;?xml version=\&#34;1.0\&#34; encoding=\&#34;utf-8\&#34;?&gt;&#34;
                           &#34;&lt;soap:Envelope xmlns:xsi=\&#34;http://www.w3.org/2001/XMLSchema-instance\&#34; xmlns:xsd=\&#34;http://www.w3.org/2001/XMLSchema\&#34; xmlns:soap=\&#34;http://schemas.xmlsoap.org/soap/envelope/\&#34;&gt;&#34;
                           &#34;&lt;soap:Body&gt;&#34;
                           &#34;&lt;Method xmlns=\&#34;http://tempuri.org/\&#34;&gt;&#34;
                           &#34;&lt;Paramener1&gt;%d&lt;/Paramener1&gt;&#34;
                           &#34;&lt;Paramener2&gt;%@&lt;/Paramener2&gt;&#34;
                           &#34;&lt;/Method&gt;&#34;
                           &#34;&lt;/soap:Body&gt;&#34;
                           &#34;&lt;/soap:Envelope&gt;&#34;,UserId,UserCode];

    NSData *postData = ;

    NSString *postLength = ];
    NSMutableURLRequest *request = [ init];
    ];
    ;
    ;
    ;
    NSLog(@&#34;URL = %@&#34;,request);
    //;
    ;
    NSURLConnection *conn = [ initWithRequest:request delegate:self];
    NSLog(@&#34;Connection link =%@&#34;,conn);
    if(conn) {
      mutableData =[ init];
      NSLog(@&#34;Connection Successful&#34;);
    } else {
      NSLog(@&#34;Connection could not be made&#34;);
    }

}

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    ;
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    ;
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    serverResponse.text = NO_CONNECTION;
    return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSXMLParser *myNSXMLParserfloorObj=[initWithData:mutableData];
    myNSXMLParserfloorObj.delegate=self;
    ;
    NSLog(@&#34;%@&#34;,myNSXMLParserfloorObj.parserError);
    NSString *responseStringWithEncoded = [ initWithData: mutableData encoding:NSUTF8StringEncoding];
    //NSLog(@&#34;Response from Server : %@&#34;, responseStringWithEncoded);
    NSAttributedString * attrStr = [ initWithData: 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()
    {
      myDataClassObj=[init];
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    myMutableStringObj=[initWithString:string];
    NSLog(@&#34;Array String: %@&#34;,myMutableStringObj);
    NSData *data = ;
    responsedict = ;
    NSLog(@&#34;JSON DATA = %@&#34;,responsedict);


}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 objective-c中将 SOAP 数据发布到服务器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38074657/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38074657/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 objective-c 中将 SOAP 数据发布到服务器