1. Fernando Leal
  2. PowerBuilder
  3. Sunday, 12 April 2020 13:00 PM UTC

Hello everyone, Could you help me convert the following C # code in PowerBuilder 2019?

http://labelary.com/service.html#csharp

 

byte[] zpl = Encoding.UTF8.GetBytes("^xa^cfa,50^fo100,100^fdHello World^fs^xz");

// adjust print density (8dpmm), label width (4 inches), label height (6 inches), and label index (0) as necessary
var request = (HttpWebRequest) WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
request.Method = "POST";
request.Accept = "application/pdf"// omit this line to get PNG images back
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = zpl.Length;

var requestStream = request.GetRequestStream();
requestStream.Write(zpl, 0, zpl.Length);
requestStream.Close();

try {
    var response = (HttpWebResponse) request.GetResponse();
    var responseStream = response.GetResponseStream();
    var fileStream = File.Create("label.pdf"); // change file name for PNG images
    responseStream.CopyTo(fileStream);
    responseStream.Close();
    fileStream.Close();
catch (WebException e) {
    Console.WriteLine("Error: {0}", e.Status);
}

 

 

thanks and regards,

Fernando.

 

Accepted Answer
Marco Meoni Accepted Answer Pending Moderation
  1. Saturday, 18 April 2020 09:32 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Fernando,

take code below just as a template as I didnt verify neither syntax nor workflow:

Blob lblb_request, lblb_response
lblb_request = Blob("^xa^cfa,50^fo100,100^fdHello World^fs^xz", EncodingUTF8!)

HttpClient http
http = Create HttpClient
// Set request headers
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
http.SetRequestHeader("Accept", "application/pdf")
http.SetRequestHeader("Content-Length", String(Len(lblb_request)))

// Send request
li_rc = http.SendRequest("POST", "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/", lblb_request)
if li_rc = 1 and http.GetResponseStatusCode() = 200 then
   http.GetResponseBody(lblb_response)

   // Write lblb_response to file with FileOpen and FileWriteEx functions
end if

 

As a reference for the Httpclient object and examples, start from here:

https://docs.appeon.com/appeon_online_help/pb2019r2/objects_and_controls/ch02s41.html

Best,

.m

Comment
  1. Fernando Leal
  2. Monday, 20 April 2020 10:51 AM UTC
Hi Marco, very good explanation.

thanks and regards.

Fernando.

  1. Helpful
There are no comments made yet.


There are replies in this question but you are not allowed to view the replies from this question.