我使用.NET 4.61。我可以从https://1106-7916.el-alt.com/wp-json/wc/v2/products/407而不是https://1106-7916.el-alt.com/wp-json/wc/v2/products获得响应,从而导致重定向吗?我可以向BuildRequest中添加代码,如下所示,以自动获得该响应吗?
我正在使用WooCommerce REST API
来创建产品。这是我的要求:
POST-https://1106-7916.el-alt.com/wp-json/wc/v2/products?consumer_key=X
{"attributes":[{"name":"Color","visible":true,"variation":true,"options":["Red","Blue","Green","Orange"]},{"name":"Size","visible":true,"variation":true,"options":["S","M","L"]}],"title":"FooFoo","sku":"TestCreateProductWoo1026","description":"Test","categories":[],"tags":[],"type":"variable"}
以下是回复:
HTTP/1.1 201 Created
Cache-Control: no-cache, must-revalidate, max-age=0
Allow: GET, POST
Location: https://1106-7916.el-alt.com/wp-json/wc/v2/products/407
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="https://1106-7916.el-alt.com/wp-json/wc/v2/products/407">here</a></body>{"id":407,"name":"Product","slug":"product"}
这是我的代码:
private string BuildRequest(string path, IDictionary<string, string> query, HttpMethodTypes httpMethod, object body)
{
string pathAndQuery = BuildParameters(path, query);
string resultData = string.Empty;
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(pathAndQuery);
myHttpWebRequest.Accept = "application/json";
myHttpWebRequest.ContentType = "application/json";
myHttpWebRequest.UserAgent = "JMA Web Technologies";
myHttpWebRequest.AllowAutoRedirect = true;
SetHeaders(myHttpWebRequest);
if (httpMethod != HttpMethodTypes.GET)
myHttpWebRequest.Method = httpMethod.ToString();
SetResponseBody(body, myHttpWebRequest);
WebResponse httpResponse = myHttpWebRequest.GetResponse();
Stream responseStream = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
resultData = reader.ReadToEnd();
responseStream.Close();
httpResponse.Close();
return resultData;
}
非常感谢您的帮助。