Post XML-file

This forum is meant for questions and discussions about the X# language and tools
Anonymous

Post XML-file

Post by Anonymous »

Dear

I want to post XML-data with a header 'user' and 'passw' and get the response.
I have created a simple example, but it is not working.

1. Is there an InternetConnect needed for user & pasw ?
2. Is the "Encoding" necessary ?

Can you improve it or is in X# an other example ?

Thanks,

Johan Kwaspen

static method DoPost() as void
local request as system.Net.WebRequest
local requestStream as system.io.Stream
local response as system.Net.WebResponse
local cpostData as string
local bytes as byte[]

cPostData := memoread("U:AppDataRoamingdtbs.xml")

// Create a request using a URL that can receive a post.
request := system.Net.WebRequest.Create("https://qqq.xxx.nl/scan/index/xml/")

bytes := System.Text.Encoding.ASCII.GetBytes(cPostData)
request.ContentType := "text/xml; encoding='utf-8'"
request.ContentLength := bytes.Length
request.Method := "POST"
requestStream := request.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
requestStream.Close()
response := request.GetResponse()

*if (response.StatusCode == HttpStatusCode.OK)
** Stream responseStream = response.GetResponseStream()
* string responseStr = new StreamReader(responseStream).ReadToEnd()
* return responseStr
*endif

return
ic2
Posts: 1813
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Post XLM-file

Post by ic2 »

Hello Johan,

A quick check with https://stackoverflow.com/questions/175 ... in-c-sharp, first answer, learned that it's about the same you do but you do not have the Close commands at the end.
If the server only grants access with a password, you have to supply. I use json exchange with the WebClient (in C#) and

using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Credentials = new NetworkCredential("user", "password");

See https://stackoverflow.com/questions/433 ... entication for more detail about credentials with XML.

Dick
JKW

Post XLM-file

Post by JKW »

Thanks Dick

I have changed the code, what do you think could work like that?

The response from the website is:
"An unexpected error occurred during the shipment"
But this can also be on the website.

thanks for the help.

Johan Kwaspen

Code: Select all

static method DoPost() as void
  local oClient         as system.net.WebClient 
  local cpostData       as string
  local XML_bytes       as byte[]                            
  local response        as byte[]                          
  
  local username        as string
  local password        as string
  local oCred           as system.Net.NetworkCredential 
  
  username := "88195"
  password := "test1234"    
  cPostData := memoread("U:AppDataRoamingTEST_dtbs.xml")   
                                                                   
  oClient        := system.net.WebClient{}
  oCred          := system.Net.NetworkCredential{"","",""}  
  oCred.domain   := "uat.webpage.be"
  oCred.username := username
  oCred.password := password                                       
  response       := null_object
  XML_bytes := System.Text.Encoding.ASCII.GetBytes(cPostData)                            

  oclient.Credentials := (oCred)      
  oClient.Headers.Add("Content-Type", "application/json")  
 
  try
    response := oClient.uploaddata("https://uat.webpage.be/scan/index/xml", "POST", ML_bytes)
  catch oException as Exception 
    System.Console.WriteLine(oException:Message)
    System.Console.WriteLine(response:ToString())
  end try        
   
User avatar
wriedmann
Posts: 3668
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Post XLM-file

Post by wriedmann »

Hi Johan,

maybe I'm totally misunderstanding: you are sending a XML file, but you are declaring it as application/json?

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
JKW

Post XLM-file

Post by JKW »

Hello Wolfgang

It should be:
But the error is the same.

Thanks !

Code: Select all

oClient.Headers.Add("Content-Type", "text/xml; encoding='utf-8'" )
User avatar
wriedmann
Posts: 3668
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Post XLM-file

Post by wriedmann »

Hi Johan,

you are using Encoding.ASCII (that is 7 bit), but are specifying it as UTF-8.

You should use Encoding.UTF8 for the GetBytes() method.

I don't know it this is the real error, but it could be.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
JKW

Post XLM-file

Post by JKW »

Hello Wolfgang

I changed it, but the same error.
When the XML is sent in UTF8-bytes, the site must also translate it back to an XML-files?

Thanks !

Johan Kwaspen
User avatar
wriedmann
Posts: 3668
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Post XLM-file

Post by wriedmann »

Hi Johan,

normally the encoding must be specified. So if you are specifying UTF8, your byte stream needs to be in UTF8, and the receiving party needs to be convert it back.

Do you can contact the other side to understand what is wrong? If they expect a JSON file and you are sending an XML, there will be an error.

Currently I'm battling with the webservice of a supplier, and if I don't send a wellformed XML, their client performs an exception with "HTTP status 500: an internal server error occured".

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
JKW

Post XLM-file

Post by JKW »

Hello Wolfgang

I will contact the other side, and see what they receive.

Is it also possible to send an XML without encoding ?

I expect them to ask for this.

I'll keep you informed

Johan
User avatar
wriedmann
Posts: 3668
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Post XLM-file

Post by wriedmann »

Hi Johan,

you need to specify the encoding because you are using a binary transport medium.
The same thing would true if you wrote to disk.

Maybe you check this topic: https://docs.xsharp.it/doku.php?id=encodings

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply