1. 程式人生 > >iOS訪問Webservice(Java伺服器)和(.net伺服器)

iOS訪問Webservice(Java伺服器)和(.net伺服器)

1.iOS訪問java的webService時出現特別多的問題

//構造一個Json型別。 這個型別很重要。一開始的時候不能帶引數。安卓的可以,後來用的是擷取傳送的資料包進行解析,

NSString *soapMessage = [NSStringstringWithFormat:

@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"

"<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>\n"

"<ns1:%@ xmlns:ns1=\"http://abc.webService.bs.xx/\">"    //藉口

"<jsonStr>%@</jsonStr>"     //屬性引數

"</ns1:%@>"

"</soap:Body>\n"

"</soap:Envelope>",method,jsonString,method];

//請求傳送到的路徑

NSString *msgLength = [NSStringstringWithFormat:@"%lu", (unsignedlong)[soapMessage

length]];

NSURL *url = [NSURLURLWithString:BaiduWebserviceURL];   //定義一個名稱空間 BaiduWebserviceURL 巨集定義

NSMutableURLRequest *urlRequest = [NSMutableURLRequestrequestWithURL:url];

//以下對請求資訊新增屬性前四句是必有的,

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

    [urlRequest

addValue: BaiduNamespaceURLforHTTPHeaderField:@"SOAPAction"];

    [urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

//非同步請求

NSURLConnection *theConnection = [[NSURLConnectionalloc] initWithRequest:urlRequest delegate:self] ;

if (theConnection) {

self.receivedData=[NSMutableDatadata];

    } else {

    }


2.iOS訪問.net的webService時

這個可以根據code4app.com 上有一個手機號碼的例子可以。就是asp的工程。