2009-08-17 17 views
13

के साथ आईफोन इंटरैक्शन मुझे एक एप्लीकेशन विकसित करना है, जो एएसपी.नेट में विकसित एक वेब सेवा का अनुरोध करेगा।एएसपी.NET वेब सेवा

मैं, पता नहीं है, क्या asp.net वेब सेवा के लिए एक अनुरोध बनाने के कोड है

कैसे asp.net वेब सेवा iPhone आवेदन का जवाब देंगे?

आईफोन कैसे प्रतिक्रिया को उचित तरीके से पार्स करेगा?

मैं पहले से ही इस सवाल का


How to Fetch Data From a WebService in iPhone?
पढ़ा है लेकिन दिए गए लिंक केवल .pdf फ़ाइल देता है।

मेरी आवश्यकता नमूना कोड है - जो मुझे बता सकता है कि कनेक्शन कैसे बनाना है & एएसपीनेट वेब सेवा से डेटा पुनर्प्राप्त करें।

उत्तर

20

आप वास्तव में एक वेब सेवा बना सकते हैं और इसे अपने आईफोन में एकीकृत करने के लिए वास्तविक बना सकते हैं। मैं सुझाव दूंगा कि क्या आप वेबएचटीपी बोली-प्रक्रिया के साथ डब्ल्यूसीएफ सेवा बनाने के लिए .NET का उपयोग कर रहे हैं और विधियों को प्राप्त और पोस्ट कर सकते हैं, आप जेसन और एक्सएमएल में प्रतिक्रियाएं प्राप्त कर सकते हैं (आईफोन पर जेसन को पार्स करने के लिए कक्षाओं का एक सेट है जो पार्सिंग करेगा एक हवा की प्रतिक्रिया, वे वेब में avaialble हैं), छोटे सेटअप के साथ आप NSURLRequest का उपयोग कर आईफोन से प्राप्त और पोस्ट करने में सक्षम होंगे। एक लेख है जो एक आरामदायक WCF सेवा http://www.developer.com/net/article.php/10916_3695436_2 बनाने के बारे में बात करता है। डब्ल्यूसीएफ के साथ आपकी सेवाओं के लिए प्रमाणीकरण और सुरक्षा जोड़ने के लिए भी बहुत आसान है।

+0

मेरे पास अपनी स्वयं की डब्ल्यूसीएफ सेवाएं चल रही हैं, मैं साबुन का उपयोग नहीं करता – Daniel

+1

मैं आपके साथ दृढ़ता से सहमत हूं, साबुन प्रोटोकॉल आईफोन – fyasar

+0

ब्रो के लिए बहुत बड़ा है, आपके द्वारा प्रदान किया गया लिंक अब उपलब्ध नहीं है। यदि आपके पास है तो नया पोस्ट करें। –

1

यदि आप दोनों तरफ एएसपी.NET के साथ एकीकृत नहीं कर रहे हैं, तो शायद मैं विशेष रूप से 'वेब सेवा' से बचूंगा, और केवल अपने स्वयं के एक्सएमएल प्रारूप को आउटपुट करूँगा, और इसे आईएमएल पक्ष पर आईएमएल पक्ष पर उचित तरीके से संसाधित करूंगा ।

3
- (void)viewDidLoad { 
[super viewDidLoad]; 

// create a soap Message which is given in your required web service 

NSString *[email protected]"<?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/\">\n" 
"<soap:Body>\n" 
"<GetCategory xmlns=\"http://tempuri.org/\" />\n" 
"</soap:Body>\n" 
"</soap:Envelope>"; 

// create a url to your asp.net web service. 
NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.32.10/Itavema/Admin/Itavema_Service.asmx"]]; 

// create a request to your asp.net web service. 
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl]; 

// add http content type - to your request 
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

// add SOAPAction - webMethod that is going to be called 
[theRequest addValue:@"http://tempuri.org/GetCategory" forHTTPHeaderField:@"SOAPAction"]; 

// count your soap message lenght - which is required to be added in your request 
NSString *msgLength=[NSString stringWithFormat:@"%i",[soapMessage length]]; 
// add content length 
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 

// set method - post 
[theRequest setHTTPMethod:@"POST"]; 

// set http request - body 
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

// establish connection with your request & here delegate is self, so you need to implement connection's methods 
NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

// if connection is established 
if(con) 
{ 
    myWebData=[[NSMutableData data] retain]; 
    // here -> NSMutableData *myWebData; -> declared in .h file 
} 
} 

// a method when connection receives response from asp.net web server 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
[myWebData setLength: 0]; 
} 
// when web-service sends data to iPhone 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[myWebData appendData:data]; 
} 
// when there is some error with web service 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
[connection release]; 
} 
// when connection successfully finishes 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
// check out your web-service retrieved data on log screen 
NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",theXML); 
[theXML release]; 

// if parser isn't nil. here NSXMLParser *myXMLParser; in .h file 
if(myXMLParser) 
{ 
    [myXMLParser release]; 
} 

// supply your responded data to xmlParser - xmlParser will parse xmlData & then you can use it 
myXMLParser = [[NSXMLParser alloc] initWithData: myWebData]; 

// here delegate self means implement xmlParse methods 
[myXMLParser setDelegate: self]; 

[myXMLParser setShouldResolveExternalEntities: YES]; 

// parse method - will invoke xmlParserMethods 
[myXMLParser parse]; 
[connection release]; 
[myWebData release]; 
} 


//#pragma mark xmlParser 
// suppose <myDataTag>myData</endmyDataTag> is the xmlData 
// this function will read "<myDataTag>" & tag attributes 
-(void)parser:(NSXMLParser*)parser 
          didStartElement:(NSString*)elementName 
          namespaceURI:(NSString*)namespaceURI 
          qualifiedName:(NSString*)qualifiedName 
          attributes:(NSDictionary*)attributeDict 
{ 
    if([elementName isEqualToString:@"GetCategoryResult"]) 
    { 
     // here categoryArray is NSMutable array declared in .h file. 
     // init your array when root element/document element is found 
     CategoryArray=[[NSMutableArray alloc]init]; 
    } 
    else if([elementName isEqualToString:@"Prop_Category"]) 
    { 
     aCategory=[[Category alloc] init]; 
     // if a tag has attribues like <myDataTag id="sagar"> 
     //aCategory.ID=[attributeDict objectForKey:@"id"]; 
    } 
} 

// suppose <myDataTag>myData</endmyDataTag> is the xmlData 
// this function will read "myData" & tag attributes 
-(void)parser:(NSXMLParser*)parser 
          foundCharacters:(NSString*)string 
{ 
    // here currentElementValue is an NSMutableString declared in .h file 
    // store read characters in that mutable string & then add to your object. 
    if(!currentElementValue) 
    { 
     currentElementValue=[[NSMutableString alloc] initWithString:string]; 
    } 
    else 
    { 
     [currentElementValue appendString:string]; 
    } 
} 

// suppose <myDataTag>myData</endmyDataTag> is the xmlData 
// this function will read "</endmyDataTag>" & tag attributes 
-(void)parser:(NSXMLParser*)parser 
      didEndElement:(NSString*)elementName 
      namespaceURI:(NSString*)namespaceURI 
      qualifiedName:(NSString*)qualifiedName 
{ 
    if([elementName isEqualToString:@"GetCategoryResult"]) 
    { 
     // if end of root element is found- i.e. end of your xml file. 
     return; 
    } 
    else if([elementName isEqualToString:@"Prop_Category"]) 
    { 
     // end of a single data element 
     // suppose <category> 
     //    <id>10</id> 
     //    <name><sagar></name> 
     //   </category> 
     // when we found </category> we have finished entire category object. 
     // here we have an object aCategory -> created custom class "Category" 
     // CategoryClass -> NSString *name; NSInteger id; 
     // Note: "important" 
     //->class variables must be same as tag 

     // now after reading entire object add to your mutable array 
     // now this mutable array can be used for Table, UIPicker 
     [CategoryArray addObject:aCategory]; 
     [aCategory release]; 
     aCategory=nil; 
     [CategoryTable reloadData]; 
    } 
    else 
    { 
     // which is equivalent to aCategory.id=10 & [email protected]"sagar" 
     [aCategory setValue:currentElementValue forKey:elementName]; 

     // remove previously read data 
     [currentElementValue release]; 
     currentElementValue=nil; 
    } 
} 
3

Hessian एक्सएमएल तुलना में काफी बेहतर संचार प्रोटोकॉल है। एक बाइनरी प्रारूप होने के नाते यह और भी कॉम्पैक्ट है, और सख्त प्रारूप पार्सिंग के साथ अधिक तेज है।

बोनस के रूप में जावा, .NET और PHP के लिए वेब सेवा का पर्दाफाश करने के लिए पहले से ही ढांचे हैं। वास्तव में आसान है। मान आप इस सी # इंटरफेस है:

public interface ITest { 
    public string getGreeting(); 
    int addNumbers(int a, int b); 
} 

तो यह सर्वर HessianC# के प्रयोग पर एक तस्वीर लागू कर रहा है:

public class CTest:CHessianHandler, ITest { 
    public string getGreeting() { return "Hello World!"; } 
    public int addNumbers(int a, int b) { return a + b; } 
    [STAThread] 
    private static void Main(string[] args) { 
    CWebServer web = new CWebServer(5667, "/test/test.hessian", typeof (CTest)); 
    web.Paranoid = true; 
    web.AcceptClient("[\\d\\s]"); 
    web.Run(); 
    for (;;) { 
     if (Console.ReadLine() != "") { 
     web.Stop(); 
     break; 
     } 
    } 
    } 
} 

iPhone पक्ष सी # इंटरफ़ेस एक ऑब्जेक्टिव-सी प्रोटोकॉल में अनुवाद करने की जरूरत पर :

@protocol ITest 
-(NSString*)getGreeting; 
-(int)addNumbers:(int)a :(int)b; 
@end 

और फिर सेवा के लिए एक प्रॉक्सी प्राप्त करने के लिए HessianKit का उपयोग कर के रूप में लगभग आसान है:

id<ITest> proxy = [CWHessianConnection proxyWithURL:serviceURL 
              protocol:@protocol(ITest)]; 
NSLog(@"Greeting: %@", [proxy getGreeting]); 
NSLog(@"The answer: %d", [proxy addNumbers:40 :2]); 

इस संक्षिप्त उत्तर में विधि नाम काफी सी # -श नहीं हैं, जो कि ओबज-सी-आश नहीं है। ऐसा इसलिए है क्योंकि डिफ़ॉल्ट रूप से हेसियनकिट जावा के नामकरण सम्मेलनों का उपयोग करता है। यह विधि प्रदान करके हेसियनकिट में ओवरराइड किया जा सकता है, और नाम अनुवाद टाइप करें। ताकि कनेक्शन पर सी # और ओब्जे-सी दोनों पक्ष घर पर 100% महसूस करते हैं।उदाहरण के लिए:

मैं:

[CWHessianArchiver setClassName:@"com.mycompany.ITest" 
        forProtocol:@protocol(CWTest)]; 
[CWHessianArchiver setMethodName:@"AddNumbers" 
        forSelector:@selector(addInt:toInt:)]; 
0

कोशिश WCF साथी है, यह आप easilly एक सोप वेब सेवा जो आपको कहीं

1

इस जवाब बाहर दिनांकित है से कॉल कर सकते हैं, लेकिन प्रोटोकॉल की खातिर बनाने की अनुमति देता मैं अपनी वेब सेवा को उद्देश्य-सी कक्षाओं में बदलने के लिए sudzc.com का उपयोग कर रहा हूं।

यह वास्तव में अच्छी तरह से काम करता है, और चीजों को काफी सरल बनाता है।

चीयर्स, ओडेड।

संबंधित मुद्दे