REST service does not work
Posted: Tue May 01, 2018 3:14 pm
With our converted VO program to X# we have a problem with a REST service which is supposed to send an XML file with PDF link to a web address, using a C# function as a replacement for an old Visual Objects function. The PDF file is uploaded via FTP and the XML is giving location info.
We get a response back that this is successful httpResponse.StatusCode = 200 (implying success), however no PDF is uploaded and we also get back the result message "disallowed key characters". This error suggest that the web address contains invalid characters, however, the address is not only the same as in the VO program, it completely consists of lower case letters - not even a number or other character.
Nor the XML file, or the web address has changed since the old Visual Objects function and still looks the same. In VO we use a method called:
METHOD GetDocumentByGetOrPost(cIP, cDocument, cData, cHeader, cMethod, nP, nFlags) CLASS cHttp
which is from Norbert Kolb, he wrote that in 2003. This VO code works without problems. Basically it goes through these calls:
SELF:hConnect := InternetConnect(parameters)
....
IF SELF:hConnect <> NULL_PTR
SELF:__SetStatusObject()
SELF:hRequest := HttpOpenRequest(parameters)
...
IF HttpAddRequestHeaders
...
HttpSendRequest(SELF:hRequest, ;
...
cRet := SELF:GetResponse() // -->cRet is successful.
The equivalent .Net code looks a lot more compact with far less of the above steps and that would be great - if it worked.
The XML is:
<Root>
<Documents>
<Document>
<Location>(location info of the PDF uploaded via FTP)</Location>
<ShipmentID>id</ShipmentID>
<Title>Cmr</Title>
</Document>
</Documents>
</Root>
This XML file is supposed to move a PDF to the website/program, however, it does not. The web address and password are correct (and the same as generated by the VO code).
Below is our C# function. We use X# to make the XML file, this pretty much works the same as the old Visual Objects function, since we converted this with the X# converter.
Did anybody encounter problems and find a solution which we can apply too?
The next step we can try is to revert to the original VO code of 2003, but one of the reasons to move to X# is that we want to rely on modern & compact .Net code. Problem is that most of the times, the old fashioned VO code works while the .Net alternative does not
Dick
[hr]
public bool PostXml(string cXml, string cAdres,string cSecret, string cHeader)
{
try
{
var httpWebRequest = (System.Net.HttpWebRequest)WebRequest.Create(cAdres);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept = "text/xml";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential(cAdres, cSecret);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(cXml);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd(); // this returns 'disallowed characters in key'
int nStatusCode = (int)httpResponse.StatusCode; // this returns 200 which implies success
}
return true;
}
catch (Exception e)
{
return false;
}
}
We get a response back that this is successful httpResponse.StatusCode = 200 (implying success), however no PDF is uploaded and we also get back the result message "disallowed key characters". This error suggest that the web address contains invalid characters, however, the address is not only the same as in the VO program, it completely consists of lower case letters - not even a number or other character.
Nor the XML file, or the web address has changed since the old Visual Objects function and still looks the same. In VO we use a method called:
METHOD GetDocumentByGetOrPost(cIP, cDocument, cData, cHeader, cMethod, nP, nFlags) CLASS cHttp
which is from Norbert Kolb, he wrote that in 2003. This VO code works without problems. Basically it goes through these calls:
SELF:hConnect := InternetConnect(parameters)
....
IF SELF:hConnect <> NULL_PTR
SELF:__SetStatusObject()
SELF:hRequest := HttpOpenRequest(parameters)
...
IF HttpAddRequestHeaders
...
HttpSendRequest(SELF:hRequest, ;
...
cRet := SELF:GetResponse() // -->cRet is successful.
The equivalent .Net code looks a lot more compact with far less of the above steps and that would be great - if it worked.
The XML is:
<Root>
<Documents>
<Document>
<Location>(location info of the PDF uploaded via FTP)</Location>
<ShipmentID>id</ShipmentID>
<Title>Cmr</Title>
</Document>
</Documents>
</Root>
This XML file is supposed to move a PDF to the website/program, however, it does not. The web address and password are correct (and the same as generated by the VO code).
Below is our C# function. We use X# to make the XML file, this pretty much works the same as the old Visual Objects function, since we converted this with the X# converter.
Did anybody encounter problems and find a solution which we can apply too?
The next step we can try is to revert to the original VO code of 2003, but one of the reasons to move to X# is that we want to rely on modern & compact .Net code. Problem is that most of the times, the old fashioned VO code works while the .Net alternative does not
Dick
[hr]
public bool PostXml(string cXml, string cAdres,string cSecret, string cHeader)
{
try
{
var httpWebRequest = (System.Net.HttpWebRequest)WebRequest.Create(cAdres);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept = "text/xml";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential(cAdres, cSecret);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(cXml);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd(); // this returns 'disallowed characters in key'
int nStatusCode = (int)httpResponse.StatusCode; // this returns 200 which implies success
}
return true;
}
catch (Exception e)
{
return false;
}
}