C#HTTP请求401和500错误

时间:2017-01-06 22:07:05

标签: c# api header webrequest walmart-api

我一直在研究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是由walmart提供的jar文件生成的签名 时间也是从jar文件生成的 CorID是随机生成的数字  而user是用户ID。

这是描述标头参数的链接。我错过了标题中的内容吗?

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

1 个答案:

答案 0 :(得分:0)

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

以下的回复是针对除Feed之外的任何请求。我将从下面列出的示例开始,以熟悉您需要如何设置标头。这适用于大多数Walmart接口,但如果请求是feed样式请求,您将需要为multipart / form-data问题提出更好的解决方案,使用Java SDK或等待C#SDK。如果有人读到这个并且有关如何通过C#提交Feed的更好答案,我很乐意听到它!

以下是有效的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;
    }
}