B4J Question What is the best soluton to post the xml-code

schimanski

Well-Known Member
Licensed User
Longtime User
Hello and happy new year!

After spending much hours, I'm now able to push an blackberry-device with the following code. I use the google-chrome poster add-on with the following URL, header und body. What would be the best solution, to post the xml-code from my jserver?

Thanks for help...

URL:
B4X:
https://cpXXXX.pushapi.eval.blackberry.com/mss/PD_pushRequest

Headers:
B4X:
Content-Type:multipart/related; boundary=PMasdfglkjhqwert;type=application/xml
Authorization:Basic xxxXXXXxxxxXXXXXsN283MzpOQWZOVnJONg==

Content-Body:
B4X:
--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8

<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
  <push-message push-id="999999999"  source-reference="XXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="2X192XXX"/>
    <quality-of-service delivery-method="confirmed"/>
  </push-message>
</pap>
--PMasdfglkjhqwert

{

     "key1":"value1",

      "key2":"value2",

     "key3":"value3",

      "key4":"value4"

}

--PMasdfglkjhqwert–-
 

schimanski

Well-Known Member
Licensed User
Longtime User
Puhh, thanks for answer. I have now switch my server to okhttp, but I don't even have an idea, how to solve my problem with multipart requests. I didn't found something in the community, that matches to my problem.
Perhaps there is someone who can show a smal example?:D
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the relevant code from PostMultipart:
B4X:
If NameValues <> Null And NameValues.IsInitialized Then
     For Each key As String In NameValues.Keys
       Dim value As String = NameValues.Get(key)
       Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${key}"

${value}
"$
       b = s.Replace(CRLF, eol).GetBytes("UTF8")
       stream.WriteBytes(b, 0, b.Length)
     Next
   End If
You might need to modify it a bit so the request looks exactly like the request you posted. There are no keys in the example you posted so you can remove it.

It is not difficult you just need to format the string and send it.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have tried to make a simple string to test, if the code sends the push to the blackberry-server. I always get the following respone:

<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"><pap><badmessage-response code="2000" desc="Received content type is not &apos;application/xml&apos;" bad-message-fragment="Received content type is not &apos;application/xml&apos;"/></pap>

What does it mean, content type is not &apos?

Copy the string to the Chrome-Poster, the push is send correctly.

B4X:
    Dim job As HttpJob
    job.Initialize("BlackberryPush", Me)
    job.PostMultipartXML("https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest")

B4X:
public Sub PostMultipartXML(Link As String)
   
    Dim stream As OutputStream
    stream.InitializeToBytesArray(0)
    Dim b() As Byte
    Dim eol As String = Chr(13) & Chr(10)
   
        Dim s As String = _
$"--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
  <push-message push-id="999999999"  source-reference="xxxx-79790aak2e2rm24M988518o8i04205l7o73"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="2A126B39"/>
    <quality-of-service delivery-method="confirmed"/>
  </push-message>
</pap>
--PMasdfglkjhqwert

{"key1":"value1", "key2":"value2", "key3":"value3"}
--PMasdfglkjhqwert–-"$
       
    b = s.Replace(CRLF, eol).GetBytes("UTF8")
   
    Log(s)
   
    stream.WriteBytes(b, 0, b.Length)
    PostBytes(Link, stream.ToBytesArray)
    req.SetContentType("multipart/related; boundary=PMasdfglkjhqwert;type=application/xml")
    req.SetContentEncoding("UTF8")
    req.SetHeader("Authorization", "Basic GHR456gkdg46373hd7gf4gfgJtMjRNOTg4NTE4bzhpMDQyMDVsN283MzpOQWZOVnJONg==")
   
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
you should read the error Received content type is not &apos;application/xml&apos;

as Received content type is not 'application/xml'

The &apos; is just a way of denoting ' (an apostrophe) in html.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Oh, thank you. :D
So the string I have posted is not accepted as xml, right?

I ask me, if my solution is basically false or is it only a little problem with the format?
Thanks for help...
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Did somebody have an idea, why the posted string is not accepted as xml? I have changed everything, but nothing helps....

B4X:
public Sub PostMultipartXML(Link As String)
   
    Dim stream As OutputStream
    stream.InitializeToBytesArray(0)
    Dim b() As Byte
    Dim eol As String = Chr(13) & Chr(10)
   
        Dim s As String = _
$"--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
  <push-message push-id="999999999"  source-reference="xxxx-79790aak2e2rm24M988518o8i04205l7o73"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="2A126B39"/>
    <quality-of-service delivery-method="confirmed"/>
  </push-message>
</pap>
--PMasdfglkjhqwert

{"key1":"value1", "key2":"value2", "key3":"value3"}
--PMasdfglkjhqwert–-"$
       
    b = s.Replace(CRLF, eol).GetBytes("UTF8")
   
    Log(s)
   
    stream.WriteBytes(b, 0, b.Length)
    PostBytes(Link, stream.ToBytesArray)
    req.SetContentType("multipart/related; boundary=PMasdfglkjhqwert;type=application/xml")
    req.SetContentEncoding("UTF8")
    req.SetHeader("Authorization", "Basic GHR456gkdg46373hd7gf4gfgJtMjRNOTg4NTE4bzhpMDQyMDVsN283MzpOQWZOVnJONg==")
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Sorry, that I have to post the same question again, but I have spend a lot of hours to post the xml-code correctly, with no result.

When I copy and paste the following string to the xml-poster add-on of Google Chrome and the httpRequester of Firefox, the push will received on the blackberry. But my MultipartXML-B4J-Poster always receives the following job.getsring:

<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"><pap><badmessage-response code="2000" desc="Received content type is not &apos;application/xml&apos;" bad-message-fragment="Received content type is not &apos;application/xml&apos;"/></pap>

I think, that the string is correct. It seems, that the way I post it is not. Perhaps there is someone who knows another way to manage the problem...

B4X:
Dim job As HttpJob
job.Initialize("BlackberryPush", Me)
job.PostMultipartXML("https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest")

B4X:
public Sub PostMultipartXML(Link As String)
  Dim b() As Byte
  Dim eol As String = Chr(13) & Chr(10)
  
  Dim s AsString = _
$"--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
 <push-message push-id="999999999" source-reference="xxxx-79790aak2e2rm24M988518o8i04205l7o73" deliver-before-timestamp="2020-12-31T23:59:59Z"> <address address-value="2A126B39"/>
 <quality-of-service delivery-method="confirmed"/>
 </push-message>
</pap>
--PMasdfglkjhqwert

{"key1":"value1", "key2":"value2", "key3":"value3"}
--PMasdfglkjhqwert–-"$
       
    b = s.Replace(CRLF, eol).GetBytes("UTF8")
    'b=s.GetBytes("UTF-8")   <------I also tried this

    PostBytes(Link,b)
    req.SetContentType("multipart/related; boundary=PMasdfglkjhqwert; type=application/xml")
    req.SetHeader("Authorization", "Basic GHR456gkdg46373hd7gf4gfgJtMjRNOTg4NTE4bzhpMDQyMDVsN283MzpOQWZOVnJONg==")
End Sub
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Kk, thanks!

I have tried it it with Fiddler Web Debugger. .It is possible to capture the postings of the browser, even it is https.
But I use the above code in a testapp under b4j. Is there something special to do to capture the postings of the b4i compiler?
There is no network traffic of b4j shown...
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Yes, that works. I think, we are just before a solution:).

The posts of the browser and the multipart-post are identical with only one exceptional:

When I post is with b4j, Fiddler tells me, that the "Request body is encoded". I made two screenshots of the both results:
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Sorry, my mistake. I have deleted this line:

B4X:
req.SetContentEncoding("UTF8")

and now, the two posts are identical....seems, that it is not that easy....
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have checked the headers of both. Under the post of b4j, I can't find the info of the Content-Type. Is it possible, that

B4X:
req.SetContentType("multipart/related; boundary=PMasdfglkjhqwert;type=application/xml")

is not enough? It seems that the content-type is not considered?


That is the RAW of the browser:

B4X:
POST https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest HTTP/1.1
Host: cpxxxx.pushapi.eval.blackberry.com
Connection: keep-alive
Content-Length: 524
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
Origin: chrome-extension://cdjfedloinmbppobahmonnjigpmlajcd
Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXzhpMDQyMDVsN283MzpOQWZOVnJONg==
Content-Type: multipart/related; boundary=PMasdfglkjhqwert;type=application/xml
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: JSESSIONID=B90C5D7F541440FC1AB883806544D4A4; BIGipServerbpds-eval-pushrequest-cld-content-orion-8080-pool=3206353836.36895.0000

--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
<push-message push-id="999999999"  source-reference="xxxx-79790aak2e2rm24M988518o8i04205l7o73"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="3A134C91"/>
<quality-of-service delivery-method="confirmed"/>
</push-message>
</pap>
--PMasdfglkjhqwert

{"key":"value"}
--PMasdfglkjhqwert–-

And that the raw of b4j:

B4X:
POST https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest HTTP/1.1
Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXzhpMDQyMDVsN283MzpOQWZOVnJONg==
Content-Length: 536
Host: cpxxxx.pushapi.eval.blackberry.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/2.4.0

--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
<push-message push-id="999999999"  source-reference="xxxx-79790aak2e2rm24M988518o8i04205l7o73"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="3A134C91"/>
<quality-of-service delivery-method="confirmed"/>
</push-message>
</pap>
--PMasdfglkjhqwert

{"key":"value"}
--PMasdfglkjhqwert–-
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Here is the small project. To make it easier, I made it with the sensitive values. It is only a test-account.
Thank you for help...


<I have deleted the project>
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have tried it, but the same result. Is it an invalid content-type? For me it looks like, that the content-type is not created.
 
Upvote 0
Top