我正在尝试使NSURLConnection与WCF * .svc基于soap的服务(不是REST)同步,并且我在httpResponse对象上不断收到 415 Media type not supported error 。我相信我正在使用正确的内容类型和 SOAPAction 设置标题,如 Fiddler 所示。我还用WebRequest编写了一个.net应用程序,我用相同的肥皂从它获得200状态OK响应。任何人都可以告诉我我哪里出错或者我错过了什么。我用我的Objective-c代码打开了ARC。谢谢你......
-(void) GetJSONResponse_soap: (NSString *) PReqURL
{
NSLog(@"URL sent: %@", PReqURL);
NSMutableString *sRequest = [[NSMutableString alloc] init];
//creating soap request
[sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
[sRequest appendString:@"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"];
[sRequest appendString:@"<s:Body>"];
[sRequest appendString:@"<GetJSONStrFromPrm xmlns=\"WCFJSON\">"];
[sRequest appendString:@"<Prm>Whatever</Prm>"];
[sRequest appendString:@"</GetJSONStrFromPrm>"];
[sRequest appendString:@"</s:Body>"];
[sRequest appendString:@"</s:Envelope>"];
NSURL *reqURL = [NSURL URLWithString:PReqURL];
//request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:reqURL];
[request addValue:@"WCFJSON/IJSONStrService/GetJSONStrFromPrm" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset:utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"\n\nRequest Xml: %@ \n\n", sRequest);
NSURLResponse *theResponse = NULL;
NSError *theError;
//Connection start
NSData *LcReceivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
//received string
NSString *ResponseStr = [[NSString alloc] initWithData:LcReceivedData encoding:NSUTF8StringEncoding];
NSLog(@"\n\nResponse: %@", ResponseStr);
//response string which in this case seems to be 415
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)theResponse;
long statusCode = [httpResponse statusCode];
NSLog(@"\n\nStatus Code: %ld",statusCode);
NSLog(@"\n\nError: %@", [theError localizedDescription]);
}