【问题标题】:WCF Serice - Getting 400 Bad Request but can use IE to browse to itWCF 服务 - 收到 400 错误请求,但可以使用 IE 浏览它
【发布时间】:2017-10-09 22:57:35
【问题描述】:

我编写了一个被 Winforms 应用程序调用的 WCF 服务。

我已经在本地测试过从 WinForms 应用程序调用 WCF 服务,一切正常

我已将 WinForms 应用程序和 WCF 服务移至远程服务器,在远程服务器上我可以使用 IE 浏览服务,但当我尝试使用 Winforms 应用程序时,我收到 400 Bad Response 错误。

我的本​​地机器和远程服务器的配置完全相同,Windows 防火墙、用户帐户等以及服务和 Winforms 应用程序的代码库/配置文件都是相同的。

WCF 服务的配置文件如下(我不得不删除基地址)

<bindings>

  <webHttpBinding>
    <binding name="webBinding">
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>

</bindings>

<services>
  <service behaviorConfiguration="CodeLocksAPIServiceBehavior" name="CodeLocksAPI.WCF.CodeLocksAPIService">
    <host>
      <baseAddresses>
      </baseAddresses>
    </host>
    <endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="CodeLocksAPI.WCF.ICodeLocksAPIService" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="CodeLocksAPIServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

在 Winforms 应用程序配置中,我尝试添加无用的 DefaultProxy 选项卡

使用以下代码从代码调用 WCF 服务

string serviceUserName = ConfigurationManager.AppSettings["ServiceUserName"];
string servicePassword = ConfigurationManager.AppSettings["ServicePassword"];

HttpClientHandler handler = new HttpClientHandler
{
    Credentials = new
             System.Net.NetworkCredential(serviceUserName, servicePassword)
};

this.Client = new HttpClient(handler);
this.Client.BaseAddress = new Uri(this.GetBaseAddressFromConfig());
processInitializeLockRequestArgs.AssetRef = assetRef;

string contentMessage = Newtonsoft.Json.JsonConvert.SerializeObject(processInitializeLockRequestArgs);
byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(contentMessage);

ByteArrayContent content = new ByteArrayContent(contentBytes);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

httpResponse = this.Client.PostAsync(methodUri, content).Result;

如前所述 - 此代码在本地工作,但不在远程服务器上 - 可以在本地和远程服务上浏览 WCF 服务

我被告知问题很可能归结为 WinForms 应用程序上的代理设置,并且问题不归结为防火墙或类似的东西

【问题讨论】:

  • 您正在尝试将凭据与您的请求一起传递,而在配置中 clientCredentialType 为 None。您可以尝试不在 HttpClient 中传递处理程序吗?
  • 其他方法是尝试将配置更改为 windows 并保留代码原样
  • 我不能使用 TLS 安全作为它唯一的内部调用,所以通过 HTTP 而不是 HTTPS 运行它
  • 然后可以将httpsGetEnabled设置为false

标签: c# wcf


【解决方案1】:

你可以试试添加

<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true"/>
  </system.net>
....

到您的 WinForms 配置文件的开头。

【讨论】:

  • 谢谢,但已经尝试过了,尝试添加它自己和这个 都没有成功
  • 很公平!代理不是我的专业科目,但这个已经把我整理了好几次了! :-)
猜你喜欢
  • 2012-05-21
  • 2012-04-27
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2014-01-11
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多