为什么收到此错误:远程服务器返回错误:(417)预期失败 [英] Why do I receive this error: The remote server returned an error: (417) Expectation Failed

查看:210
本文介绍了为什么收到此错误:远程服务器返回错误:(417)预期失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个朋友向我展示了此示例代码以在C#中实现HTTP POST,并在WINFORM应用程序中成功实现了: http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/

A friend show me this sample code to implement HTTP POST in C# and did work out in a WINFORM App: http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/

并在METRO APP中实现:

And implemented in a METRO APP:

 // this is what we are sending
string post_data = "user=user@example.com&pass=example123";

// this is where we will send it
string uri = "http://app.proceso.com.mx/win8/login";

// create a request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";

// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(post_data);

// this is important - make sure you specify type this way
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = await request.GetRequestStreamAsync();

// now send it
requestStream.Write(postBytes, 0, postBytes.Length);

// grab te response and print it out to the console along with the status code
WebResponse response = await request.GetResponseAsync();
//var a = new StreamReader(response.GetResponseStream()).ReadToEnd();
StreamReader requestReader = new StreamReader(response.GetResponseStream());
String webResponse = requestReader.ReadToEnd();

我意识到HttpWebRequest不包含ProtocolVersion,并且在此行中向我抛出此错误:

I realized, HttpWebRequest does not contain ProtocolVersion and is throwing me this error in this line:

WebResponse response = await request.GetResponseAsync();
// ERROR: The remote server returned an error: (417) Expectation Failed.

我想最后一个属性就是解决方案.我怎么解决这个问题? 预先感谢

I guess the last property is the solution. How can I solve this problem? Thanks in advance

推荐答案

我最近编写了一个小函数来处理琐碎的数据到服务器的发布.

I recently wrote a small function to handle the posting of trivial data to a server.

private struct HttpPostParam
{
    private string _key;
    private string _value;

    public string Key { get { return HttpUtility.UrlEncode(this._key); } set { this._key = value; } }
    public string Value { get { return HttpUtility.UrlEncode(this._value); } set { this._value = value; } }

    public HttpPostParam(string key, string value)
    {
        this._key = key;
        this._value = value;
    }
};

private static string PostTrivialData(Uri page, HttpPostParam[] parameters)
{
    string pageResponse = string.Empty;
    try
    {
        var request = (HttpWebRequest)WebRequest.Create(page); //create the initial request.
        request.Method = WebRequestMethods.Http.Post; //set the method
        request.AllowAutoRedirect = true; //couple of settings I personally prefer.
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded";

        //create the post data.
        byte[] bData = Encoding.UTF8.GetBytes(string.Join("&", Array.ConvertAll(parameters, kvp => string.Format("{0}={1}", kvp.Key, kvp.Value))));
        using (var reqStream = request.GetRequestStream())
            reqStream.Write(bData, 0, bData.Length); //write the data to the request.

        using (var response = (HttpWebResponse)request.GetResponse()) //attempt to get the response.
            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NotModified) //check for a valid status (should only return 200 if successful)
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                    pageResponse = reader.ReadToEnd();
    }
    catch (Exception e)
    {
        /* todo: any error handling, for my use case failing gracefully was all that was needed. */
    }
    return pageResponse;
}

基本上,它发布在"parameters"参数中定义的值对.需要引用并导入System.Web命名空间才能进行编译.

Essentially it posts the value pairs defined in the "parameters" argument. Will require a reference and import of the System.Web namespace to compile.

我刚刚在您的网站上对其进行了测试,并得到了回复:

I just tested it with your website and got a response back:

HttpPostParam[] httpparams = {
                                new HttpPostParam("user", "censored@email.com"),
                                new HttpPostParam("pass", "example123")
                             };
string response = PostTrivialData(new Uri("http://app.proceso.com.mx/win8/login"), httpparams);

让我知道是否有任何问题.

Let me know if there's any issues.

这篇关于为什么收到此错误:远程服务器返回错误:(417)预期失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆