C# WebClient not posting data correctly to PHP script -


c#:

using (webclient client = new webclient()) {     byte[] response = client.uploadvalues("http://example.com/api/server/key",                                           new namevaluecollection() {                                               { "key", key }                                           });      string result = encoding.utf8.getstring(response);     console.writeline(result); } 

php:

$key = $_post["key"]; echo($key); 

when runs, php $_post array never has value posted it. meaning echo line has no output.

also, yes, 'key' have value, cropped out in example.

i not sure, expression new namevaluecollection() { ... } think should do.

you should explicit ,

namevaluecollection values = new namevaluecollection(); values.add("key", key); byte[] response = client.uploadvalues("http://example.com/api/server/key",                                       values); 

searching object initializers reveals collection initializers

collection initializers let specify 1 or more element initializers when initialize collection class implements ienumerable or class add extension method.

and example

list<cat> cats = new list<cat> {     new cat(){ name = "sylvester", age=8 },     new cat(){ name = "whiskers", age=2 },     new cat(){ name = "sasha", age=14 } }; 

so maybe using initializer without parentheses works, e.g.

byte[] response = client.uploadvalues("http://example.com/api/server/key",                                       new namevaluecollection {                                           { "key", key }                                       }); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -