The advice of Skydiver for my previous question directed me to HttpClient, and I'm now successfully sending a message to my CICS web service and receiving a response with the code below. This is based on code from an HttpClient tutorial
How do I convert this so that line 22 waits for the response, throwing an error if the request times out or there are other problems.
My current tests use debugging to step through Post after it has been initiated from a button's click event (below) , but control leaves JSPG2Client.Post immediately line 22 executes.
I'd really like to change the signature of the Post method to
public Post(IJSPG2 ijspg2, ref OJSPG2 ojspg2)
to simplify the interface as much as possible.
Thank you for your help,
Robert.
How do I convert this so that line 22 waits for the response, throwing an error if the request times out or there are other problems.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using System.Text;
namespace MyJSv
{
public class JSPG2Client
{
static readonly HttpClient client = new HttpClient();
public async Task Post(IJSPG2 ijspg2)
{
try
{
var json = JsonConvert.SerializeObject(ijspg2);
json = "{\"JSPG2\" : {\"IJSPG2\" : " + json + "}}";
var data = new StringContent(json, Encoding.UTF8, "application/json");
var url = "http://localhost:9003/cics/services/JSPG2";
var response = await client.PostAsync(url, data);
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
}
My current tests use debugging to step through Post after it has been initiated from a button's click event (below) , but control leaves JSPG2Client.Post immediately line 22 executes.
C#:
private void btnEnquire_Click(object sender, EventArgs e)
{
ijspg2.JZ_Function = "E";
ijspg2.JZ_Employee.EMPNO = txtEmpno.Text;
jspg2Client.Post(ijspg2);
}
public Post(IJSPG2 ijspg2, ref OJSPG2 ojspg2)
to simplify the interface as much as possible.
Thank you for your help,
Robert.