重构一个接口以使用当前使用通用System.ServiceModel.ClientBase的HttpWebRequest

时间:2011-11-01 13:01:41

标签: .net wcf httpwebrequest x509

我目前正致力于重构一个接口(.net wcf windows service),它将数据发送到基于java的轴服务。

我希望新的预订能够使用http web请求将一段xml发送到当前在基于wcf的界面的app.config中配置的url。

到目前为止,我已经从旧界面捕获了日志记录,并创建了一个新界面,能够将该数据发布到网址。基本上,这是有效的。轴服务的响应是无法验证令牌:

<faultstring>WSDoAllReceiver: security processing failed; 
nested exception is:    org.apache.ws.security.WSSecurityException: 
The security token could not be authenticated or authorized</faultstring>

在旧界面中,app.config中的绑定信息如下所示:

  <endpoint address="https://beta.<<>>.com/ws/services/ReservationService"
    behaviorConfiguration="" binding="customBinding" bindingConfiguration="MutualCertificateBinding"
    contract="ReservationServiceContract" name="" kind="" endpointConfiguration="">
    <identity>
      <certificateReference x509FindType="FindByThumbprint" findValue="27 38 6f 86 b6 67 45 be 1a 60 60 ef 1f 7c 73 d6 5b 1b 7d 7f"
        isChainIncluded="false" />
    </identity>
  </endpoint>

我使用以下代码将请求发送到Web服务:

    private string HandleHttpPost(string url, UTF8Encoding encoding, string xmlMsg)
    {
        System.Net.ServicePointManager.Expect100Continue = false;

        string request = textBoxRequestPrefix.Text + xmlMsg;
        byte[] data = encoding.GetBytes(request);

        var webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Method = WebRequestMethods.Http.Post;
        webRequest.ContentType = textBoxRequestContentType.Text;

        if(!string.IsNullOrEmpty(textBoxSoapAction.Text))
            webRequest.Headers.Add("SOAPAction", textBoxSoapAction.Text);

        if (!string.IsNullOrEmpty(textBoxCertificatePath.Text))
        {
            X509Certificate2 xc = new X509Certificate2(textBoxCertificatePath.Text);
            webRequest.ClientCertificates.Add(xc);
        }

        Stream newStream = webRequest.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        newStream.Close();

        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        Stream dataStream = webResponse.GetResponseStream();
        var reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();

        reader.Close();
        dataStream.Close();
        webResponse.Close();

        return responseFromServer;
    }

这告诉我正在使用x509证书。我的问题是,我如何使HttpWebRequest工作?

添加:自定义绑定配置:

<customBinding>
<binding name="MutualCertificateBinding" closeTimeout="00:05:00" openTimeout="00:05:00" sendTimeout="00:05:00">
  <security authenticationMode="UserNameOverTransport" securityHeaderLayout="Lax"
    includeTimestamp="false" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
    requireSignatureConfirmation="false">
    <secureConversationBootstrap />
  </security>
  <textMessageEncoding maxReadPoolSize="64" messageVersion="Soap11WSAddressing10" writeEncoding="utf-8">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
  </textMessageEncoding>
  <context protectionLevel="None" />
  <httpsTransport maxBufferPoolSize="524288" maxReceivedMessageSize="65536" maxBufferSize="65536" />
</binding>
</customBinding>

0 个答案:

没有答案