“远程服务器返回错误:(417)期望失败。”

时间:2012-10-03 06:50:38

标签: c# .net wcf firewall

我正在编写使用我的wcf服务的Windows 8应用程序(在Visiual Studio 2012上)。当我在家里尝试时它很好用,所以它可以连接到wcf。但是,当我在我的办公室尝试时,它无法连接到wcf并返回错误:

  

远程服务器返回错误:(417)期望失败。

我认为它是由办公网络中的防火墙引起的.. 谷歌搜索太多,尝试了很多,但问题仍然在这里。

System.Net.ServicePointManager.Expect100Continue = false;

不起作用,因为.Net Framework 4.5没有ServicePointManager类,但msdn说.Net 4.5上有ServicePointManager .. http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.expect100continue.aspx

<system.net>
    <settings>
        <servicePointManager expect100Continue="false" />
    </settings>
</system.net>

无法尝试在web.config或app.config上编写,因为win8应用程序没有这些文件..

有些人将上面的代码写到了VS 2012的devenv.exe.config文件中。我试过但没有改变。

http://www.jlpaonline.com/?p=176

3 个答案:

答案 0 :(得分:1)

嗯,您发布了问题here,而Can Bilgin给出了答案。 我在这里发帖给那些遇到问题的人。

  

我认为应该可以手动添加标题...我会的   从前面的例子开始,给它一个操作范围:

Task<GetADGroupMemberResponse> AccountManagement.GetADGroupMemberAsync(GetADGroupMemberRequest request)
{
    // CB: Creating the OperationContext
    using (var scope = new OperationContextScope(this.InnerChannel))
    {
        // CB: Creating and Adding the request header
        var header = MessageHeader.CreateHeader("Server", "http://schemas.microsoft.com/2008/1/ActiveDirectory/CustomActions", request.Server);
        OperationContext.Current.OutgoingMessageHeaders.Add(header);

        return base.Channel.GetADGroupMemberAsync(request);
    }
}
  

因为使用httpclient的http请求你可以做点什么   喜欢(再次来自前面的例子):

var httpClient = new HttpClient(); 
var httpContent = new HttpRequestMessage(HttpMethod.Post, "http://app.proceso.com.mx/win8/login");

// NOTE: Your server was returning 417 Expectation failed, 
// this is set so the request client doesn't expect 100 and continue.
httpContent.Headers.ExpectContinue = false;

var values = new List<KeyValuePair<string, string>>(); 
values.Add(new KeyValuePair<string, string>("user", "******"));
values.Add(new KeyValuePair<string, string>("pass", "******"));

httpContent.Content = new FormUrlEncodedContent(values);

HttpResponseMessage response = await httpClient.SendAsync(httpContent);

// here is the hash as string 
var result = await response.Content.ReadAsStringAsync();

答案 1 :(得分:0)

作为参考,以下是使用WebClient将JSON请求发布到REST Web服务的完整示例。它将Expect100Continue标志设置为false,包括使用默认凭据进行代理身份验证(如果需要),并捕获非200响应代码。

void Main()
{
    var ms = new MemoryStream();
    var ser = new DataContractJsonSerializer(typeof(Job));
    ser.WriteObject(ms, new Job { Comments = "Test", Title = "TestTitle", Id = 1 });

    var uri = new Uri("http://SomeRestService/Job");
    var servicePoint = ServicePointManager.FindServicePoint(uri);
    servicePoint.Expect100Continue = false;

    var webClient = new WebClient();
    webClient.Headers["Content-type"] = "application/json";

    IWebProxy defaultProxy = WebRequest.DefaultWebProxy;
    if (defaultProxy != null){
        defaultProxy.Credentials = CredentialCache.DefaultCredentials;
        webClient.Proxy = defaultProxy;
    }

    try {
        var result = webClient.UploadData(uri, "POST", ms.ToArray());
    } catch (WebException we) {
        var response = (HttpWebResponse)we.Response;
    }
}

[DataContract]
public class Job
{
    [DataMember (Name="comments", EmitDefaultValue=false)]
    public string Comments { get; set; }
    [DataMember (Name="title", EmitDefaultValue=false)]
    public string Title { get; set; }
    [DataMember (Name="id", EmitDefaultValue=false)]
    public int Id { get; set; }
}

答案 2 :(得分:0)

虽然添加此行
()  如果您的代理设置不允许您使用在目标服务器上打开的正确打开端口,则作为配置的设置可能会修复您可能仍会获得的初始错误(504)网关超时。要为要发送的服务器添加代理例外,请通过Internet Explorer将其添加为连接选项卡上的代理排除项 - &gt;局域网设置 - &gt;高级。我希望你能够连接起来。