【问题标题】:Trying to upload with a PUT request尝试使用 PUT 请求上传
【发布时间】:2016-11-15 00:47:17
【问题描述】:

我正在尝试使用网站 API 更改文章的价格 它的文档位于https://www.mkmapi.eu/ws/documentation/API_1.1:Stock

运行该类时,我收到错误 417 Expectation Failed,文档中将其描述为: 当您的请求有一个没有相应标头的 XML 正文和/或正文不是以文本形式发送,而是以字节表示形式发送时,通常您会收到 417 Expectation Failed HTTP 状态代码。出现 417 的另一个可能原因是,当您发送超过 1.024 字节的正文数据而未在请求中添加标头 Expect: 时。

任何帮助将不胜感激。我也应该说认证不是问题,我可以下载我的文章价格。

public void UpdateMarketPrice(string MarketID, string NewPrice) 
{
    // https://www.mkmapi.eu/ws/documentation/API_1.1:Stock

    String finalResult;
    String method = "PUT";
    String url = "https://www.mkmapi.eu/ws/v1.1/stock";

    HttpWebRequest request = WebRequest.CreateHttp(url) as HttpWebRequest;
    OAuthHeader header = new OAuthHeader();
    request.Headers.Add(HttpRequestHeader.Authorization, header.getAuthorizationHeader(method, url));
    request.Method = method;
    request.ContentType = "text/xml; encoding='utf-8'";

    XElement xmlDoc = 
        new XElement("request",
            new XElement("article",
                new XElement("idArticle", MarketID),
                new XElement("idLanguage", 1),
                new XElement("comments", "Edited through the API"),
                new XElement("count", 7),
                new XElement("price", 11),
                new XElement("condition", "NM"),
                new XElement("isFoil", false),
                new XElement("isSigned", false),
                new XElement("isPlayset", false)
            )
        );

    String finalXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + xmlDoc.ToString();
    MessageBox.Show(finalXML);
    byte[] bytes = Encoding.ASCII.GetBytes(finalXML);
    request.ContentLength = bytes.Length;

    using (Stream putStream = request.GetRequestStream())
    {
        putStream.Write(bytes, 0, bytes.Length);
    }

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        finalResult = reader.ReadToEnd();
    }

    MessageBox.Show(finalResult);
}

【问题讨论】:

    标签: c# winforms put


    【解决方案1】:

    我读到 HttpWebRequest 会在请求中添加一个“expect 100 continue”标头,除非您将其关闭。有些服务器可能不支持此标头。并且会产生这个 417 Expectation Failed 消息。

    您可以尝试将其设置为 false:

    System.Net.ServicePointManager.Expect100Continue = false;
    

    所以标题没有发送。

    我也看到了其他类似问题的建议解决方案。

    【讨论】:

    • 效果惊人!非常感谢!
    • 很高兴听到它有效!实际上我昨天在 mkmapi 上遇到了同样的问题,但是由于我在工作,所以我没有时间自己尝试解决方案。
    【解决方案2】:

    也许使用StreamWriter

    using (Stream putStream = request.GetRequestStream())
    {    
        using (var writeStream = new StreamWriter(putStream, Encoding.ASCII))
        {
            writeStream.Write(finalXML);
        }
        request.ContentLength = putStream.Length; // I am not sure about that 
    }
    

    【讨论】:

    • 感谢您的建议 Slai,我尝试了您的代码,但现在我收到另一个错误 System.NotSupportedException。附加信息:此流不支持查找操作。
    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 2021-12-19
    • 2021-01-06
    • 1970-01-01
    • 2021-09-10
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多