尝试使用 PUT 请求上传
Trying to upload with a PUT request
我正在尝试使用网站更改文章的价格 API
它的文档位于 https://www.mkmapi.eu/ws/documentation/API_1.1:Stock
当 运行 和 class 时,我收到错误 417 Expectation Failed,文档中将其描述为:
当您的请求具有 XML body 而没有相应的 header and/or body 时,通常您会收到 417 Expectation Failed HTTP 状态代码不是作为文本发送,而是它的字节表示。 417 的另一个可能原因是,当您发送 body 超过 1.024 字节的数据时,没有添加 header 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);
}
也许使用 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
}
我读到 HttpWebRequest 会向请求添加 "expect 100 continue" header,除非您将其关闭。有些服务器可能不支持此 header。并将产生此 417 Expectation Failed 消息。
您可以尝试将其设置为 false:
System.Net.ServicePointManager.Expect100Continue = false;
因此 header 未发送。
我也看到了针对其他类似问题的建议解决方案。
我正在尝试使用网站更改文章的价格 API 它的文档位于 https://www.mkmapi.eu/ws/documentation/API_1.1:Stock
当 运行 和 class 时,我收到错误 417 Expectation Failed,文档中将其描述为: 当您的请求具有 XML body 而没有相应的 header and/or body 时,通常您会收到 417 Expectation Failed HTTP 状态代码不是作为文本发送,而是它的字节表示。 417 的另一个可能原因是,当您发送 body 超过 1.024 字节的数据时,没有添加 header 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);
}
也许使用 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
}
我读到 HttpWebRequest 会向请求添加 "expect 100 continue" header,除非您将其关闭。有些服务器可能不支持此 header。并将产生此 417 Expectation Failed 消息。
您可以尝试将其设置为 false:
System.Net.ServicePointManager.Expect100Continue = false;
因此 header 未发送。
我也看到了针对其他类似问题的建议解决方案。