I have a cURL command provided by WooCommerce:
curl https://example.com/wp-json/wc/v2/orders \
-u consumer_key:consumer_secret
I'm using WebClient:
using (WebClient wc = new WebClient())
{
Uri url = new Uri("https://www.myhost.com/wp-json/wc/v2/orders");
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("consumer_key", keyValue);
byte[] responsebytes = wc.UploadValues(url, "POST", reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
But I'm getting this error:
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
What have I done wrong here? I'm pretty sure my url and key are correct.
EDIT: Have just tried this as suggested by CmdrTchort and David:
using (WebClient wc = new WebClient())
{
wc.UseDefaultCredentials = true;
wc.Credentials = new NetworkCredential("consumer_key", keyValue);
string responsebody = wc.DownloadString("https://www.myhost.com/wp-json/wc/v2/orders");
}
Still receiving the same error.
EDIT: I have a suspicion I've been provided with invalid credentials I will update once this becomes clear...
I was given the wrong credentials. However that has not solved the issue.
I don't understand how consumer_key:consumer_secret
is supposed to be represented in this request. There are two values: consumer_key and consumer_secret (I was only supplied with consumer_secret before, which I placed where keyValue
is). I now assume that it is supposed to be of the form:
wc.Credentials = new NetworkCredential("consumer_key", "consumer_secret");
where "consumer_key" and "consumer_secret" represent the unique values provided. This does not work. This is starting to get slightly irritating.