2012-10-03 148 views
6

我正在写Windows 8应用程序(在Visiual Studio 2012上),它使用我的wcf服务。当我在家中尝试时,它运行良好,因此它可以连接到wcf。但是当我试图在我的办公室,它无法连接到WCF和返回错误:”远程服务器返回错误:(417)期望失败。“

The remote server returned an error: (417) Expectation Failed.

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

不起作用,因为.Net框架4.5还没有ServicePointManager类,但MSDN说,有ServicePointManager在.NET 4.5 .. 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

+2

在我的Web服务调用被代理阻止之前,我看到过这个错误 – lockstock

回答

1

嗯,你张贴的问题hereCan Bilgin作出了回答。 我在这里发布给那些比你有问题的人。

I think it should be possible to add the header manually... I would give it a go with the operationscope... from an earlier example:

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); 
    } 
} 

cause on the http requests with the httpclient you can do something like (again from an earlier example):

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(); 
0

作为参考,下面是使用Web客户端来发布JSON请求到web服务REST的一个完整的例子。它将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; } 
} 
0

虽然加入这一行
() 作为设置到配置可以固定初始误差,你很可能仍然得到(504)网关超时,如果你的代理服务器设置不允许你使用正确的打开目标服务器上的端口。要为您要发送的服务器添加代理例外,请通过Internet Explorer将其添加为“连接”选项卡上的代理排除 - > LAN设置 - >高级。我希望你能够连接。

相关问题