REST: Add message to azure storage queue without b

2019-05-05 18:58发布

问题:

I don't have the possibility to encode my request to base64, and according to the documentation I shouldn't have to, but I can't figure it out.

If I Base64 encode it's working fine:

<QueueMessage>
<MessageText>PHNhbXBsZT5zYW1wbGUgbWVzc2FnZTwvc2FtcGxlPg==</MessageText>
</QueueMessage>

Which adds the decoded message to the queue:

<sample>sample message</sample>

According to the documentation (https://msdn.microsoft.com/sv-se/library/azure/dd179346.aspx)

A message must be in a format that can be included in an XML request with UTF-8 encoding. To include markup in the message, the contents of the message must either be XML-escaped or Base64-encode. Any XML markup in the message that is not escaped or encoded will be removed before the message is added to the queue.

Trying to add (instead of PHNhbXBsZT5zYW1wbGUgbWVzc2FnZTwvc2FtcGxlPg==):

&lt;sample&gt;sample message&lt;/sample&gt;

Succeeds but when trying to view the message in the queue it only responds with:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

Does somebody know how send a proper raw xml-escaped request message to the storage queue?

Full request (without keys and names):

POST https://xxxxxxx.queue.core.windows.net/testqueue/messages?sv=2015-04-05&ss=q&srt=sco&sp=a&se=2026-11-11T20:24:03Z&st=2016-11-11T12:24:03Z&spr=https&sig=xxxxxxxxxxxxxxxxx%3D HTTP/1.1
User-Agent: Fiddler
Host: XXXXX.queue.core.windows.net
Content-Type: text/plain
Content-Length: 64

<QueueMessage>
<MessageText>&lt;sample&gt;sample message&lt;/sample&gt;</MessageText>
</QueueMessage>

回答1:

I was getting format errors calling the AsString property because the message returned from the storage queue was, by default, base64 encoded.

The CloudQueue object has an EncodeMessage property that was set to true. Switched it to false and everything worked. Here's my code:

 CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
 CloudQueue queue = queueClient.GetQueueReference(ClientQueueName);
 queue.EncodeMessage = false;
 CloudQueueMessage retrievedMessage = queue.GetMessage();

ClientQueueName being a string holding the name of my queue on Azure. I'm thinking that the property needs to be set to false in your case before sending as well.



标签: rest azure queue