2009-06-04 43 views
11

Trong dự án của chúng tôi, chúng tôi có một dịch vụ web java chạy trên http và https. Chúng tôi muốn sử dụng http nội bộ và https cho phiên bản bên ngoài của ứng dụng web của chúng tôi.gọi một dịch vụ web sử dụng WCF qua HTTP và HTTPS

Vì vậy, chúng tôi đã tạo các lớp proxy trong ứng dụng của chúng tôi và chúng tôi đã thiết lập các ràng buộc cho http trong web/app.config và tất cả hoạt động tốt.

gì thay đổi chúng tôi sẽ cần phải thực hiện với mã và cấu hình để hỗ trợ https cho dịch vụ tương tự trong ứng dụng bên ngoài của chúng tôi? Nếu có thể, vui lòng cung cấp đoạn mã để giải thích!

Trả lời

0

Xin xem Configuring HTTP and HTTPS:

Using Windows Communication Foundation (WCF) over HTTP either requires the use of a host, such as Internet Information Services (IIS), or manual configuration of the HTTP settings through the HTTP Server API. This document describes manually configuring WCF when using HTTP and HTTPS.

Và cũng thấy WCF Bindings Needed For HTTPS:

I just finished writing my first production WCF application, which worked very well until I deployed it to our production environment. All of a sudden none of the WCF calls would work, and I would get a JavaScript "TestService is not defined" error. When I look inside the JS service reference (in debug mode), I got the following error:

Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https]

So apparently my WCF service registered itself as HTTPS (since it is over SSL), but my binding was only configured for HTTP. The solution is to define a custom binding inside your Web.Config file and set the security mode to "Transport". Then you just need to use the bindingConfiguration property inside your endpoint definition to point to your custom binding. The entire HTTPS-enabled system.serviceModel section is below:

0

Tôi hiểu rằng bạn đang sử dụng WCF để xây dựng khách hàng, kết nối với một dịch vụ web từ xa, qua HTTPS.

Để thực hiện việc này, chỉ cần sửa đổi tệp cấu hình phía máy khách cho ứng dụng WCF hỗ trợ, thay thế http://server.address bằng https://server.address, trong configuration/system.serviceModel/client/endpoint/@address. như vậy:

<configuration> 
    <system.serviceModel> 
    ... 
    <client> 
     <!-- change only the address string --> 
     <endpoint address="https://server.name/Whatever" 
      everything.else.stays.the.same /> 

    </client> 
    </system.serviceModel> 
</configuration> 

(Đường dẫn đến tập tin cấu hình thay đổi tùy theo các quy tắc NET thường xuyên: cho dù đó là một ứng dụng ASPNET, hoặc một dịch vụ, hoặc vv)

HOẶC bạn có thể đặt địa chỉ rõ ràng trong mã:

// instantiate new proxy to web service 
    var svc= new ServiceClient(); 
    var e = svc.Endpoint; 
    e.Address = new System.ServiceModel.EndpointAddress("https://server.address/JavaServiceUri"); 

Tôi khuyên bạn nên đặt cấu hình địa chỉ và không mã hóa địa chỉ. Điều đó không có nghĩa là nó phải được lưu trữ trong app.config, nhưng nó phải được thay đổi. Các proxy, quá.

+0

này không làm việc cho hoàn cảnh của tôi, nó kết quả trong một hệ thống. ArgumentException với thông báo "Lược đồ URI được cung cấp 'https' không hợp lệ; dự kiến ​​'http'." – RenniePet

2

Tôi giả định rằng bạn đang sử dụng basicHttpBinding. Sau đó, bạn cần phải làm hai việc:

  • thay đổi địa chỉ để https :)
  • thiết lập chế độ bảo mật để vận chuyển
+0

đẹp - liên kết đến cùng một câu hỏi :) – Rory

+0

@Rory, bắt thú vị, câu trả lời đã có hơn một năm, bạn là người đầu tiên chú ý :) –

+0

Rõ ràng là câu trả lời của bạn là, nó chỉ lưu cho tôi có khả năng giờ. 'Tôi giả định rằng bạn đang sử dụng basichttpbinding.' SLL của tôi đã làm việc cho SOAP, nhưng nó không hoạt động cho webHttpBinding -REST. Bây giờ tôi đã xác định được vấn đề, tôi có thể bắt đầu sửa chữa :) – Doomsknight

22

Tôi tìm thấy một câu trả lời đào bới xung quanh MSDN.

Trong trường hợp của tôi, tôi đã sử dụng một tùy chỉnh ràng buộc:

<customBinding> 
    <binding name="jsonpBinding"> 
     <jsonpMessageEncoding/> 
     <httpTransport manualAddressing="true"/> 
    </binding> 
</customBinding> 

đó đã được nhắc đến trong các dịch vụ

<services> 
    <service name="{YourInfoHere}"> 
     <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/> 
    </service> 
</services> 

Thêm một ràng buộc thứ hai sử dụng httpsTransport và sau đó một dịch vụ thứ hai mà sử dụng mà ràng buộc đã làm các trick. Sản lượng cuối cùng:

<services> 
     <service name="{YourInfoHere}"> 
      <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/> 
      <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBindingHttps" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/> 
     </service> 
    </services> 
    <bindings> 
     <customBinding> 
      <binding name="jsonpBinding"> 
       <jsonpMessageEncoding/> 
       <httpTransport manualAddressing="true"/> 
      </binding> 
      <binding name="jsonpBindingHttps"> 
       <jsonpMessageEncoding/> 
       <httpsTransport manualAddressing="true" /> 
      </binding> 
     </customBinding> 
    </bindings> 

Có thể không lý tưởng, nhưng nó hoạt động. Đây là những thay đổi duy nhất tôi thực hiện để thực hiện SSL. Vì nó là tất cả trong việc vận chuyển & ràng buộc, mã vẫn giữ nguyên.

liên quan liên kết MSDN:

  1. Tuỳ chỉnh Binding: http://msdn.microsoft.com/en-us/library/ms731377.aspx
  2. HttpTransport: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httptransportbindingelement.aspx
  3. HttpsTransport: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httpstransportbindingelement.aspx
+3

+1 -Chính xác những gì tôi đang tìm :) –

Các vấn đề liên quan