Hi Please can you help. I have been asked to see if I can integrate our systems with a 3rd party tool.
I've had the details from the 3rd party and it turns out I need to upload some data to a JSON Web service, then obviously manipulate the results
Example php has been supplied, but I always work in c# or VB and Ive made a start but starting to feel lost, can you help ?
''
public function runJsonRequestTest() {
$client = new \SoapClient(
"https://website.wsdl",
array(
'trace' => 1,
'exception' => 0
)
);
try {
$response = json_decode(
$client->json_request(
‘username’,
‘password’,
json_encode( //The request array
[
‘category’ => ‘api’,
‘type’ => ‘getMethods’,
‘data’ => [ ]
]
)
),
true
);
}
catch(\Exception $e) {
return "Soap request failed: {$e->getMessage()}
" . $client->__getLastResponse();
}
$string = print_r($response, true);
return $String;
'''
I've also got
'''
Example request array:
[
‘category’ => ‘api’,
‘type’ => ‘getMethods’,
]
Example return output:
Array (
[status] => COMPLETE
[data] => Array (
[0] => methodA
[1] => methodB
[2] => methodC
[3] => methodD) )
'''
so far I've got the below - but I get many errors and starting to get a bit lost - the below does not compile !
'''
public class ResultsTest
{
public string status { get; set; }
public List data { get; set; }
}
public static async Task RunAsync(String Url, String Username, String Password, InstructionMessageModel instruction, String log)
{
_log = log;
_url = Url;
_username = Username;
_password = Password;
var baseAddress = new Uri(_url);
using (var client = new HttpClient())
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var auth = string.Format("{0}:{1}", _username, _password);
var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
try
{
HttpResponseMessage response = await client.PostAsJsonAsync(_url, instruction);
if (!response.IsSuccessStatusCode)
{
var errors = await response.Content.ReadAsAsync();
}
else
{
var jsonAsString = await response.Content.ReadAsAsync();
var output = JsonConvert.DeserializeObject(jsonAsString);
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
'''
Loading
matt incklePosted Jan 26, 2016, 8:11 PM
Hi Thanks for your reply.
I am currently getting the error
"No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'"
on the line
var jsonAsString = await response.Content.ReadAsAsync();
so I have tried using
var jsonAsString = response.Content.ReadAsStringAsync().Result;
This does not error but I get a string value which is basically an xml exactly the same as if I browse to the url - therefore not the result I was expecting.
It is as thought im making a call to the url but not passing any information to it.
can you recommend where to look at next
thanks for your time , any help would be massively appreciated
current code is
[code]
var baseAddress = new Uri(_url);
using (var client = new HttpClient())
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var serializer = new JavaScriptSerializer();
string testobject = serializer.Serialize(new { category = "introducerapi", type = "getMethods"});
string json = serializer.Serialize(new { username = _username, password = _password, request = testobject });
HttpResponseMessage response = await client.PostAsJsonAsync(_url, json);
if (!response.IsSuccessStatusCode)
{
}
else
{
var jsonAsString = response.Content.ReadAsStringAsync().Result;
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
[/code]
Mithun PattankarPosted Jan 25, 2016, 11:50 PM