iphone - 安全 WCF 不适用于非 Windows 客户端 iPhone

标签 iphone ios wcf soap https

我创建了 WCF SOAP 服务来与我的 iPhone 应用程序一起工作。它与 VS 中的服务客户端“编码客户端”和 WCF 测试客户端工具一起工作正常,但与 iPhone 或 Java 或 PHP 不工作。 WCF 它在 https 配置上工作

像这样绑定(bind)

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding" maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" />
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" negotiateServiceCredential="false"
          establishSecurityContext="false" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

和如下行为:

<services>
      <service behaviorConfiguration="ServiceBehavior" name="HR_Service.Service">
        <endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration=""
          name="MexHttpsBindingEndpoint" contract="HR_Service.IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"
            httpsGetBinding="" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer maxItemsInObjectGraph="6553600" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

和 ASP.net session 兼容性

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" /> 

我的iPhone是这样的

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES; // Or whatever logic
}

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host 
{ 
}

接受来自服务器的证书,然后SOAP信封

NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\" xmlns=\"http://tempuri.org/\"><soap:Body><Authenticate><userid>username</userid><password>pass</password></Authenticate></soap:Body></soap:Envelope>"];

然后是信封

NSURL *url = [NSURL URLWithString:@"https://mywebsite.com/Service/Service1.svc"];                           
    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://tempuri.org/IService1/Authenticate" forHTTPHeaderField:@"soapAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
    [theRequest setHTTPMethod:@"POST"];     
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {
        webData = [NSMutableData data] ;
    }
    else {
        NSLog(@"theConnection is NULL");
    }

NSURLConnectionDelegate

- (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(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Data has been loaded");

    NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];


    NSLog(@"Respose Data :%@",responseString) ;


}

连接总是成功但响应数据总是空的

最佳答案

你的soapMessage是错误的

你用错了标签

在 soamMessage 字符串中使用“Authenticate”标签而不是“test”标签

关于iphone - 安全 WCF 不适用于非 Windows 客户端 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666951/

相关文章:

java - 从 Java 学习 iPhone 编程的最佳方法是什么?

iphone - JSON 还是 SOAP (XML)?

iphone - Cocos2d 肖像模式在 iPhone 上不起作用

iphone - SVProgressHUD 在 vi​​ewWillDisappear 中关闭 好吗?

ios - 如何在我的 xcode 项目中添加和使用自定义字体

c# - 高可用性

c# - 以编程方式修改端点 ReaderQuotas

ios - Swift:使用 snapkit 时如何获得正确的 UICollectionView 高度?

ios - iPhone 后台计时器可以在 3GS 上使用吗?

c# - .NET Framework 4 与 .NET Framework 3.5 中的 WCF 有哪些新功能?