我正在尝试使用网站API更改文章的价格 其文档位于https://www.mkmapi.eu/ws/documentation/API_1.1:Stock
运行该类时,我收到错误417 Expectation Failed,从文档中描述如下: 通常,当您的请求具有没有相应标头的XML主体和/或未作为文本发送的主体但是其字节表示时,您将获得417期望失败的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);
}
答案 0 :(得分:2)
我已经读过HttpWebRequest添加&#34;期望100继续&#34;请求标头,除非您将其关闭。有些服务器可能不支持此标头。并将生成此417期望失败的消息。
您可以尝试将其设置为false:
System.Net.ServicePointManager.Expect100Continue = false;
所以标题没有发送。
我已经看到这也建议解决其他类似的问题。
答案 1 :(得分:0)
也许使用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
}