HTTP 请求导致 401 和 500 返回码

问题描述 投票:0回答:1

我一直在研究 Walmart API,但是当我运行代码时,我不断收到 401 错误或 500 错误:

 public void post()
    {
        byte[] data = Encoding.ASCII.GetBytes(
$"username={user}&password={password}");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://marketplace.walmartapis.com/v2/feeds?feedType=item");
        request.Method = "POST";
        request.Accept = "application/xml;";
        request.ContentLength = data.Length;
        request.Headers.Add("WM_SVC.NAME", "Walmart Marketplace");
        request.Headers.Add(authId);
        request.Headers.Add("WM_CONSUMER.ID", user);
        request.Headers.Add( time);
        request.Headers.Add(CorId);
        using (Stream stream = request.GetRequestStream ())
        {
            stream.Write(data , 0, data.Length);
        }

        string responseContent = null;

        using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            { 
                using (StreamReader sr99 = new StreamReader(stream))
                {
                    responseContent = sr99.ReadToEnd();
                }
            }
        }

        MessageBox.Show(responseContent);
    }

其中

authID
是从沃尔玛提供的jar文件生成的签名 时间也是从 jar 文件生成的。
CorID
是随机生成的数字 user 是用户 ID。

这是描述标头参数的链接。我是否遗漏了标题中的某些内容?

https://developer.walmartapis.com/#getting-started

c# http-headers webrequest walmart-api
1个回答
0
投票

您的请求存在多个问题。首先,您要提交一个 feed,但是当它应该是一个 multipart/form-data 请求时,将其作为 application/xml 发送。除此之外,您的标头设置不正确,并且当前使用 C# 向沃尔玛提交多部分/表单数据请求存在一个主要问题。我还没有看到任何人通过 C# 成功向沃尔玛发送 feed 的帖子。我目前正在使用 C# 执行一个批处理文件,然后触发 Walmart Java SDK 的修改版本,该版本能够发送多部分/表单数据请求。

以下回复适用于 Feed 以外的任何请求。我将从下面列出的示例开始,以熟悉如何设置标题。这适用于大多数沃尔玛接口,但如果请求是提要样式请求,您将需要为多部分/表单数据问题提出更好的解决方案,使用 Java SDK,或者等待C# SDK。如果有人阅读本文并对如何仅通过 C# 提交提要有更好的答案,我很想听听!

这里是一个有效的 application/xml 请求的示例。

string timestamp = CurrentTimeMillis().ToString().Trim();
string query = @"orders/"+poID+"/acknowledge";
string request = v3BaseUrl + query;  //Constructed URI

string stringToSign = consumerId     + "\n" +
                      request.Trim() + "\n" +
                      "POST"         + "\n" +
                      timestamp      + "\n";


string signedString = signData(stringToSign);  //Your signed string

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request);
webRequest.Accept = "application/xml";
webRequest.ContentType = "application/xml";
webRequest.Method = "POST";
webRequest.Headers.Add("WM_SVC.NAME", "Walmart Marketplace");
webRequest.Headers.Add("WM_SEC.AUTH_SIGNATURE", signedString);
webRequest.Headers.Add("WM_CONSUMER.ID", consumerId);
webRequest.Headers.Add("WM_SEC.TIMESTAMP", timestamp.ToString().Trim());
webRequest.Headers.Add("WM_QOS.CORRELATION_ID", Guid.NewGuid().ToString());
webRequest.Headers.Add("WM_CONSUMER.CHANNEL.TYPE", channelType);
webRequest.ContentLength = 0;
webRequest.Timeout = Timeout.Infinite;
webRequest.KeepAlive = true;

using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        success = true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.