Iphone 目标C中的Web服务请求

Iphone 目标C中的Web服务请求,iphone,objective-c,Iphone,Objective C,我在objective C中是新手。我已经使用以下代码在j2me和android中完成了应用程序。我试图通过objective C使用web服务,但没有成功。如果有人能指引我,那就太好了 谢谢 public static String RetriveData(String myStr) { String result1 = "-1"; Object ob1 = new Object(); ob1 =MyStr; SoapObject rpc = new

我在objective C中是新手。我已经使用以下代码在j2me和android中完成了应用程序。我试图通过objective C使用web服务,但没有成功。如果有人能指引我,那就太好了

谢谢

  public static String RetriveData(String myStr) 
{

    String result1 = "-1";
    Object ob1 = new Object();

   ob1 =MyStr;

    SoapObject rpc = new SoapObject("http://abcd.com/", "MyMethod");


    rpc.addProperty("Mystr",  ob1.toString());

    try 
    {


         Object strdata = new HttpTransport("http://11.22.33.44/myService.asmx", "http://abcd.com/" + "MyMethod").call(rpc);
        result1 = strdata.toString().trim();

    } 
    catch (Exception ex) 
    {
                  System.out.println("In catch block   :" +ex);   

    }


  return result1;

  }
我试图通过如下目标C实现同样的目标,但出现了错误

NSURL *url = [NSURL URLWithString:@"http://11.22.33.44/MyService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
//
//[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://abcd.com/MyMethod" forHTTPHeaderField:@"SOAPAction"];
//[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest addValue:@"MyStr" forHTTPHeaderField:@"MyStr"];

[theRequest setHTTPMethod:@"POST"];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    webData = [[NSMutableData data] retain];


}
else
{
    NSLog(@"theConnection is NULL");
}

[nameInput resignFirstResponder];

}

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
    [webData setLength: 0];
 }
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
 }
 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes]             length:     
 [webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];

if( xmlParser )
{
    [xmlParser release];
}

xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];

[connection release];
[webData release];
   }

我个人的建议是使用AsiTRequest。我使用它来使用web服务(包括.NET和PHP),在大多数情况下使用起来似乎更简单、更直接

只需包括ASIHttpRequest类和MBProgressHUD(如果您想使用它)

以下是我用来做这件事的方法:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Connecting to Server";

// Start request
NSURL *url = [NSURL URLWithString:@"http://mydomain/MyWebService.asmx/MyMethod"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setValidatesSecureCertificate:NO];

// Now setup the Request
[request setPostValue:@"MyValue1" forKey:@"WebServiceArg1"];
[request setPostValue:@"MyValue2" forKey:@"WebServiceArg2"];

[request setDelegate:self];
[request startAsynchronous];
现在使用委托方法检查并使用来自web服务的响应:

#pragma mark - ASIHttpRequest Delegate Methods
- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    if (request.responseStatusCode == 200) {
        NSString *responseString = [request responseString];
        // Do something with this, create an array or dictionary depending on how the return data is structured (this assumes you are using a JSON formatted return string btw
        }
        else {
            // Standard UIAlert Syntax
            UIAlertView *myAlert = [[UIAlertView alloc] 
                                    initWithTitle:@"Connection Error" 
                                    message:@"My Message"
                                    delegate:nil 
                                    cancelButtonTitle:@"OK" 
                                    otherButtonTitles:nil, nil];

            [myAlert show];

        }
    } else {
        NSLog(@"Error finishing request");
    }

}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    // Standard UIAlert Syntax
    UIAlertView *myAlert = [[UIAlertView alloc] 
                            initWithTitle:@"Connection Error" 
                            message:@"Unable to establish connection" 
                            delegate:nil 
                            cancelButtonTitle:@"OK" 
                            otherButtonTitles:nil, nil];

    [myAlert show];

    NSError *error = [request error];
    NSLog(@"%@",error.localizedDescription);
}

有很多方法可以做到这一点,但这正是我要使用的。

我遇到以下错误“服务器无法为请求提供服务,因为媒体类型不受支持。”这是HTTP 415-不受支持的媒体类型响应代码。尝试取消对“内容类型”行的注释,或为“接受”添加一行。您能找出问题吗?人们不太倾向于通过查看一堆代码来找出问题所在。请注意,ASIHTTPRequest的开发已停止,文章命名了备选方案。网络很好。是ASIHTTPRequest的另一种选择。该死!我不知道。我会调查其他人的谢谢。