c# - cannot convert from 'string' to 'System.Collections.Specialized.NameValueCollection' -


i working on asp.net mvc-4 web application , , have following method upload json object 3rd part application. want set url header application/x-www-form-urlencoded:-

using (webclient wc = new webclient())                  {                     string url = currenturl + "resources?authtoken=" + pmtoken;                     uri uri = new uri(url);                      wc.headers.add(httprequestheader.contenttype, "application/x-www-form-urlencoded");                      var encodedjson = webutility.urlencode(data);                     crudoutput = wc.uploadvalues(uri, "input_data=" + encodedjson);                 } 

but above raising following error :-

cannot convert 'string' 'system.collections.specialized.namevaluecollection'

the best overloaded method match 'system.net.webclient.uploadvalues(system.uri, system.collections.specialized.namevaluecollection)' has invalid arguments

so can adivce on please ?

the error pretty clear. uploadvalues takes namevaluecollection not string https://msdn.microsoft.com/en-us/library/9w7b4fz7(v=vs.110).aspx

you code should be

var nvc = new namevaluecollection();  nvc.add("input_data", encodedjson);  crudoutput = wc.uploadvalues(uri, nvc); 

update

you might try uploadstring instead: https://msdn.microsoft.com/en-us/library/0645045y(v=vs.110).aspx

crudoutput = wc.uploadstring(uri, "input_data=" + encodedjson); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -