fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Post XML-file

Post XML-file 1 year 4 months ago #3743

  JKW's Avatar Topic Author JKW Offline Posts: 27
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:\AppData\Roaming\dtbs.xml")

// Create a request using a URL that can receive a post.
request := system.Net.WebRequest.Create(" 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

Please Log in or Create an account to join the conversation.

Last edit: by robert.

Post XLM-file 1 year 4 months ago #3746

  ic2's Avatar ic2 Offline Posts: 499
Hello Johan,

A quick check with stackoverflow.com/questions/17535872/htt...-xml-data-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 stackoverflow.com/questions/4334521/http...basic-authentication for more detail about credentials with XML.

Dick

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3753

  JKW's Avatar Topic Author JKW Offline Posts: 27
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
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:\AppData\Roaming\TEST_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        
   

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3754

  wriedmann's Avatar wriedmann Away Posts: 1573
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann. Reason: fix typo

Post XLM-file 1 year 4 months ago #3755

  JKW's Avatar Topic Author JKW Offline Posts: 27
Hello Wolfgang

It should be:
But the error is the same.

Thanks !
oClient.Headers.Add("Content-Type", "text/xml; encoding='utf-8'" )

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3756

  wriedmann's Avatar wriedmann Away Posts: 1573
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3757

  JKW's Avatar Topic Author JKW Offline Posts: 27
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

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3758

  wriedmann's Avatar wriedmann Away Posts: 1573
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3759

  JKW's Avatar Topic Author JKW Offline Posts: 27
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

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3760

  wriedmann's Avatar wriedmann Away Posts: 1573
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3761

  JKW's Avatar Topic Author JKW Offline Posts: 27
Hello Wolfgang

Thanks for the information, am going to consult the supplier.
If it works I will let you know what the problem was.

Greetings,

Johan

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3773

  ic2's Avatar ic2 Offline Posts: 499
Hello Johan,

I'll doublecheck with my json code Thursday but first 2 remarks:

1 It seems you did reach the server and got a custom error from it. The receiving site should be able to tell you ore; it could easily be a ill-formatted XML file
2 I had a custom error as well and heard that I had to change the POST to a PUT. Maybe that's your problem as well.

Let me know that first, then I'll check further if required.

Dick

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3794

  JKW's Avatar Topic Author JKW Offline Posts: 27
Hello Dick

The POST or PUT gives this same error.
"The underlying connection is closed: An unexpected error has occurred during transmission."

In VO it will work: maybe because it works with HttpOpenRequest
and not with oClient.uploaddata

I will inform the supplier of the website.

Thanks for your reply !!

Johan

VO-code
if self:hConnect <> null_ptr
  self:__SetStatusObject()
  //  Once we've identified the service, we need to indicate the
  //  page to which the data will be posted. In this example, it will
  //  be "/getorpost.php"
  self:hRequest := HttpOpenRequest(self:hConnect, ;   // hConnect
       String2Psz("POST"), ;       // lpszVerb
       String2Psz(cDocument), ;    // pszObjectName
       String2Psz("HTTP/1.1"), ;   // pszVersion
       null_psz, ;                 // pszReferer
       null_psz, ;                 // pszAcceptTypes
       nFlags, ;                   // dwFlags
       self:__GetStatusContext())  // dwContext

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3796

  ic2's Avatar ic2 Offline Posts: 499
Hello Johan,
But is used to be the error "An unexpected error occurred during the shipment"

What you now see is something else. There's quite some info about this, e.g.:
stackoverflow.com/questions/21728773/the...occurred-on-a-receiv

Not sure why it works with VO. The whole idea is that .Net has much more built in to handle json or XML. I never programmed it in VO but it took a while before I got it running as well. Usually the server owner could see if we reached them and if so, what was wrong (e.g. something in the XML/formatting or the PUT instead of the POST, etc).

Dick

Please Log in or Create an account to join the conversation.

Post XLM-file 1 year 4 months ago #3802

  JKW's Avatar Topic Author JKW Offline Posts: 27
Hello Dick

I will consult the supplier and then see what he gets,
it can also be in a configuration issue.

Thanks for help with finding a solution !

If I succeed, I'll let you know.

Johan Kwaspen

Please Log in or Create an account to join the conversation.

  • Page:
  • 1