1

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.

Dom
  • 83
  • 7
  • Due to the error message, it looks like handling of certificate or SSL/TLS version problem or mismatch – Pac0 Oct 04 '17 at 10:25
  • 2
    you mentioned that you have the cURL command provided by them, but have you tested it from the workstation where you're trying to hit via WebClient? It might help better isolate the problem to know if the same issue occurs with cURL and guide you as to whether you should be looking at your own code or something between you and the web service. – Kyle Burns Oct 04 '17 at 10:28

2 Answers2

0

You're posting the consumer_key:consumer_secret as part of a form POST, but that's not what cURL was doing. That looks more like a GET request with supplied authentication. Perhaps something like this:

wc.Credentials = new NetworkCredential("consumer_key", "consumer_secret");

and then making a GET request instead of a POST. With no values to upload, that part might be simplified. Maybe something like:

string responsebody = wc.DownloadString(url);
David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks but still receiving the same error (see edited post) – Dom Oct 04 '17 at 10:39
  • @Dom: Have you confirmed that the cURL command itself is successful? If nothing works then you may need to check with the vendor. – David Oct 04 '17 at 10:49
0

-u in curl passes the credentials as authorized headers and not as a dictionary of params.

You can add your credentials with :

wc.UseDefaultCredentials = true; 
wc.Credentials = new NetworkCredential("username", "password");

Is your service responding and/or is it an untrusted or self-signed certificate? If so , you can test ignoring the SSL-warning.

Btw, curl uses get by default (-X specifies method, which I can't see in your example) - so I'm assuming your only receiving by default and not actually posting values? If so you can use the DownloadString() directly.

So, if you're trying to get a file from this url you can do :

// IGNORE ALL SSL Certificates - would not do this in production
ServicePointManager.ServerCertificateValidationCallback =
       new RemoteCertificateValidationCallback(
            delegate
            { return true; }
        );

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");
}
Harald F.
  • 4,505
  • 24
  • 29
  • Have tried your example (was a GET request, my mistake) but still getting same error. – Dom Oct 04 '17 at 10:44
  • Any issues with your SSL certificate? Test the above method in which case. If not - you should check if your service /orders is still working when requesting it from a browser or similar. – Harald F. Oct 04 '17 at 10:54