• State Suggested Answer
  • Date
  • Date 19 Dec 2018 19:20
  • Replies 3 replies
  • Answers 1 answer
  • Subscribers 956 subscribers
  • Views 132 views

Visual Cobol + Post + Json

Hello!

I would like to know how to use webservice (post + json) in Visual Cobol. Is this possible in VisualCobol?

Below are two examples in C # that I tried to translate, but it did not work.

Can someone help me?

 

 

First example:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
user = "Foo",
password = "Baz"
});

streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}

 

Another example:

 

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"user\":\"test\"," +
"\"password\":\"bla\"}";

streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}

 

I'm waiting. Thanks.

Marcia Pires
marcia.pires@mmsis.com.br


  • You haven't told us what version of Visual COBOL you're using. Please include product, version, and platform with each question. In this case we know you're using Visual COBOL, and I'll assume you're writing managed COBOL for .NET, since you refer to C# examples. I'll assume you're on the latest version.

    You also haven't shown us your attempts to translate the C# into managed COBOL, which might be a better starting point. That said, here's a COBOL version (off the top of my head) for the first example:

    declare httpWebRequest = WebRequest::Create("http://url") as type HttpWebRequest
    set httpWebRequest::ContentType to "application/json"
    set httpWebRequest::Method to "POST"

    perform using streamWriter = new StreamWriter(httpWebRequest::GetRequestStream)
    declare jsSerializer = new JavasScriptSerializer
    declare json as string = jsSerializer::Serialize(new MyJson(user = "Foo", password = "Baz"))
    streamWriter::Write(json);
    end-perform

    declare httpResponse = httpWebRequest::GetResponse as type HttpWebResponse
    declare result as string
    perform using streamReader = new StreamReader(httpResponse::GetResponseStream)
    set result to streamReader::ReadToEnd
    end-perform

    ...

    class-id MyJson.
    1 user public string property as "user".
    1 password public string property as "password".
    end class MyJson.

    Untested, and may require some changes to compile and work correctly. You'll need all the appropriate references and namespaces specified in the project, or (in the case of namespaces) with $set directives.

    The biggest difference here is that, as far as I know, managed COBOL does not have syntax support yet for anonymous objects, which the C# example uses when it specifies the object to be serialized to JSON. Consequently I had to define a class (MyJson) to represent that object. That class definition can be in a separate COBOL source file in the project, in the same COBOL source file as the web-service-client code as another top-level class, or within the top-level class (particularly if you don't need that definition elsewhere).

  • In reply to Michael.Wojcik:

    i have Need an good converter for json-files and have written in Cobol my own program. I know that with dotnet there are better Tools if you have the Schema Explanation. And when not? The same Problem...
  • In reply to greiner:

    If you don't have an object type corresponding to an incoming JSON message, you can use JavaScriptSerializer::DeserializeObject to deserialize it to a generic object. Then you can use reflection to query the attributes of that object.