QT 中向webservice发送xml数据请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT 中向webservice发送xml数据请求相关的知识,希望对你有一定的参考价值。

在QT5.X中使用post方法,将xml数据参数向webservice请求
我有一个地址http://21.8.25.1:86/N2/services/erp_services?wsdl
然后方法是syncTestInfo
参数是xml格式如下:
<ITEMINFOS>
<ITEMINFO>
<ITEM_SN>物料SN</ITEM_SN >
<MO_NUMBER>制令单号</MO_NUMBER>
<OPERATE_EMP>操作员</OPERATE_EMP>
<REMARK1>备注</REMARK1>
<REMARK2></REMARK2>
<REMARK3></REMARK3>
<REMARK4></REMARK4>
<REMARK5></REMARK5>
</ITEMINFO>
</ITEMINFOS>
返回参数:字符串 TRUE|FALSE:xxxxxxx

该怎么实现?

服务端向客户端传数据不会做了!这样 你是给他提供接口,他传入参数给你 你通过这些参数 在后台取得应该返回的东西 返回给他,比如 接口为
public String getUserID(String name);//这里的参数也许是个XML,那么你就需要解析这个XML,
通过name 查询数据库 返回userID;
return userID;追问

就是客户端想服务器请求,发送的数据是xml格式,以下是我写的代码,但是不行

参考技术A 好厉害的样子

如何在ios中向soap webservice发送请求?

【中文标题】如何在ios中向soap webservice发送请求?【英文标题】:How to send request to soap webservice in ios? 【发布时间】:2016-03-24 07:19:38 【问题描述】:

您好,我想向soap 网络服务发送请求。我尝试使用 AFnetworking 并获得内部服务器错误 500,现在我尝试使用 nsurlconnection 但它也显示状态代码 400。请任何人帮助我。谢谢提前听到是我的代码。

NSString *soapString = [NSString stringWithFormat:
                                    @"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><addBusOrder xmlns=\"http://tempuri.org/\"><UserUniqueID>ok//8FjcvEKWMeeJJZHKYA</UserUniqueID><PlatFormID>4</PlatFormID><DeviceID>E3D2FCF6-F41B-4275-BD34-FAA31307EFFE</DeviceID><RouteScheduleId>532875503</RouteScheduleId><JourneyDate>2016-03-25</JourneyDate><FromCityid>734</FromCityid><ToCityid>202</ToCityid><TyPickUpID>22860424</TyPickUpID><Contactinfo><Name>vinod</Name><Email>katragadda.vinod@gmail.com</Email><Phoneno>7842768497</Phoneno><mobile>8801720427</mobile></Contactinfo><pass><passenger><passengerName>kumar</passengerName><Age>23</Age><Fare>1072</Fare><Gender>M</Gender><Seatno>D3</Seatno></passenger></pass></addBusOrder></soap:Body></soap:Envelope></addBusOrder></soap12:Body></soap12:Envelope>"];
            NSLog(@"soapString %@",soapString);

    NSString *msgLength = [NSString stringWithFormat:@"%li", [soapString length]];

    NSString *queryString = [NSString stringWithFormat: @"https://asprel.in/Service/AppServices.asmx/addBusOrder"];

    NSURL *url = [NSURL URLWithString:queryString];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    [req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [req addValue:@"http://tempuri.org/addBusOrder" forHTTPHeaderField:@"SOAPAction"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];


    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:req queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) 
        if (connectionError) 
            NSLog(@"error %@",connectionError);
        else
            NSLog(@"data %@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        


        NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
        [parser parse];

    ];

错误:

status code: 500, headers 
    "Access-Control-Allow-Origin" = "*";
    "Cache-Control" = private;
    "Content-Length" = 322;
    "Content-Type" = "text/plain; charset=utf-8";
    Date = "Thu, 24 Mar 2016 07:08:32 GMT";
    Etag = "\"\"";
    Server = "Microsoft-IIS/8.0";
    "X-AspNet-Version" = "4.0.30319";

【问题讨论】:

【参考方案1】:

您需要 API 授权

尝试添加授权

 // Create the request
 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];

// New Create the connection
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sharedSession];//sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

//  NSURLCredential *creds = [NSURLCredential credentialWithUser:self.username password:self.password persistence:NSURLCredentialPersistenceForSession];

NSString *authStr = [NSString stringWithFormat:@"%@:%@",username API,password API];// @"username:password";

NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];

// Part Important
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

[request addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request addValue:@"http://tempuri.org/addBusOrder" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request     
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 

                 NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
                [parser parse];

                NSLog(@"%@",responseData);
                    if (error) 
                         [self handleError: error];
                    
 ];

[dataTask resume];

NSLog(@"Header Request--->> %@",request.allHTTPHeaderFields);

【讨论】:

以上是关于QT 中向webservice发送xml数据请求的主要内容,如果未能解决你的问题,请参考以下文章

WebService

如何在 ASP.NET MVC 中向客户端发送 XML 文件

webservice接口频繁推送

如何用webservice从客户端发送一个xml给服务端接收并插入数据库

Express - 在单个请求中向浏览器发送页面和自定义数据?

webservice面试题