I have a message in a Journal of a Private queue (.\Private$\theQueue\Journal$)
The message was created by WCF and processed (thus on the Journal).
The problem is I want to get the message (the body is too large to view in the Admin Tools) so i have created the following code
MessageQueue myQueue = new MessageQueue(txtQueueName.Text);
Message peekByLookupId = myQueue.PeekById(txtLookUpId.Text);
StreamReader reader = new StreamReader(peekByLookupId.BodyStream);
txtResult.Text = reader.ReadToEnd();
but the StreamReader does not return any result for the ReadToEnd. however the Stream does have a length (peekByLookupId.BodyStream.Length) of 1676
does any one have the code to peek at the XML of the object which WCF created (using the DataContractFormatter)
Or does anyone know where is the DataContractFormatter, as i could use this deserialise the object. (I have added the System.Runtime.Serialization to the project and it still does not recognise the object)
Many thanks
In the end I pulled out the body stream as bytes (do not convert into a string). converted into hex and viewed in WinHex..
there has to be a better way
Related
I am writing code for Request and Response web compression in vb.net using GzipStream. I need to take analysis before and after compression of Response. I have written below code to capture the Response data length after de-compression.
Using testStream As New GZipStream(response.GetResponseStream(), CompressionMode.Decompress)
Dim decompressedBytes(800000) As Byte
compLength = zippedStream.Read(decompressedBytes, 0, 800000)
WriteFileStream(compLength, Path, Length, methodURL)
Return testStream
End Using
"Stream was not readable" error is coming after Return testStream. Without zippedStream.Read code everything is working fine. "WriteFileStream" is just a method which writes the data into a file.
Any suggessions here.
Regards
sangeetha
I am creating utiltiy which send mail using my outlook account, to do so I am creating object of javax.mail.Message and send it, if message sending is failed due to SendingFailedException, I want to add those messages into jms queue, and at the other end listener will run at every 10 min interval to consume these messages from the queue and try to resend those messages.
I have gone through some of the stackoverflow topics related to same, they instructed to change message into xml or in JSON, I just want to know how to deal with it, if that would be the way to implement this.
Thanks in advance
Using the MimeMessage.writeTo method you can turn the message into a byte stream. Collect it in a ByteArrayOutputStream and then include the bytes in the JMS message. At the other end, you can reconstitute the message using the MimeMessage constructor that takes an InputStream.
For example:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
msg.writeTo(bos);
byte[] data = bos.toByteArray();
// put the data in a JMS message
// in the receiver, extract the byte array from the message
byte[] data = ...
MimeMessage msg = new MimeMessage(session, data);
Sorry, I can't help you with the JMS part.
Original Question
I'm using an API to get a thumbnail image that I uploaded. Instead of providing me with a url to the image, the service returns me a garbles text string that I thought was Base64. However, all of my attempts to decode this string have failed. Does anyone have any ideas of what object types the service might be returning me? If it's not base64, what would it be?
screenshot on imgur of the API response
Answer to Original Question
#Andrew Tran: pointed out that the response I was getting looked like it was the raw binary data for the png file. This helped correct my lack of experience with Base64 and led me to some further research.
First Attempt at downloading the file
Dim path As String = "C:\Users\username\lpImages\img1.png"
Dim fs As FileStream = File.Create(path)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(thumbnail)
fs.Write(info, 0, info.Length)
fs.Close()
This never worked... so I talked with a coworker and finally realized my mistake
I had been using an abstracted class to call the API and had glazed over the fact that under the hood the "request" method was actually reading the return stream into a string. The service returned the image png file as a stream, but the request method was converting this raw stream. Once I created a different request method I was able to get the png as a stream. From there it was relatively easy to use it as I intended: attaching the images as LinkedResources to an email that I then send out.
Original request method code
response = theRequest.GetResponse
Dim reader As StreamReader = New StreamReader(response.GetResponseStream)
lp_response.response = reader.ReadToEnd
New request code I wrote instead
lp_response.response = theRequest.GetResponse.GetResponseStream
VB.Net code to handle the stream (this is just a snippet where I'm building a List of LinkedResources to pass to my email function; just to give an idea of how I'm using it)
Dim document As Stream = LPApi.GetDocumentThumbnail(d("id").ToString)
Dim mediaType As String = Utils.GetContentType(Path.GetExtension(d("file_name")))
Dim lrDocument As New LinkedResource(document, mediaType)
lrDocument.ContentId = d("id").ToString
Thanks to everyone who commented. I'm pretty inexperienced when it comes to the deeper architecture of web requests/responses and data serialization. Any good learning resources would be helpful; otherwise I'll just keep Googling :)
It doesn't look like it's being encoded at all.
The IHDR chunk start is clearly visible and in plaintext at the start of the file.
It's probably not a good idea to use HttpWebRequest to download binary data, then convert the response to a string, just to try to convert that string back into binary data to save it to a file.
If you're getting an HttpWebResponse object, you should be able to use HttpWebResponse.GetResponseStream to get a stream for the image data. Then you should be able to copy the image data straight into your filestream using Stream.CopyTo.
I am sending POST data to PHP webpage from my windows app using VB.net. The code is as follows:
Dim data As String = "this is a test data push"
Dim wc As New WebClient
wc.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
wc.UploadStringAsync(uri, data)
The data gets stored. In the PHP page, I am sending an XML response back to the phone
header('Content-type: text/xml');
echo '';
echo '<count>';
echo '<',$i,'>'
echo '</count>';
echo '';
How do I use WebClient to read the RESPONSE back? Any help would be appreciated. Thanks alot.
You need to subscribe to the UploadStringCompleted event. See http://msdn.microsoft.com/en-us/library/ms144239.aspx for more details, but here is an excerpt:
This method sends a string to a resource. The string is sent
asynchronously using thread resources that are automatically allocated
from the thread pool. Before uploading the string, this method
converts it to a Byte array using the encoding specified in the
Encoding property. To receive notification when the string upload
completes, you can add an event handler to the UploadStringCompleted
event.
Drilling farther in, you can see that even returns a UploadStringCompletedEventArgs object that includes the results via the Result property. See http://msdn.microsoft.com/en-us/library/system.net.uploadstringcompletedeventargs.aspx for more details.
I am writing a WCF client to a SOAP service that returns a mime multi-part result with binary data (actually, a PDF file). It uses a custom message encoder.
The service doesn't seem to mind if I make the request a single-part format, so I am able to get a result back. There are two problems with the result from what I can see:
It only seems to return the first part of the multi-part message.
The data I get back cannot be decoded by my custom encoder.
I have tried utilizing MTOM binding, but that messes up the request. It fails to add the "boundary" parameter in the content-type, so the server cannot understand the request.
I think what I want is a basic text SOAP request, but a response decoded MTOM-style. I have no idea how to set that up, however.
The closest solution I have found is this: http://blogs.msdn.com/b/carlosfigueira/archive/2011/02/16/using-mtom-in-a-wcf-custom-encoder.aspx
But it seems like a very invasive change to my project.
I figured this out. First of all, I was incorrect when I said that I was only getting the first part of the multi-part message using the MTOM encoder; I was getting the whole thing. I was looking at it in the debugger and the bottom must have gotten clipped in the debug viewer. Chalk it up to my inexperience manually looking at and deciphering multi-part messages.
To the second point, all I had to do was use the MTOM encoder when the Content-Type was multipart/related and everything worked just fine. If you read the referenced article above, it's all about dynamically detecting whether the message is multipart or regular text, and choosing the proper encoder based on that. Essentially, it's a custom encoder that has both a text encoder and MTOM encoder built into it, and switches back and forth based on the content-type of the incoming message.
Our project requires some post processing of the response message before it's handed off to the main program logic. So, we get the incoming SOAP content as an XML string, and do some XML manipulation on it.
This is a slight departure from the solution recommended in the article. All that's required in the article's solution is reading the message using the right encoder into a System.ServiceModel.Channels.Message, and returning that. In our solution, we need to interrupt this process and do the post-processing.
To do that, implement the following in your custom encoder:
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
//First, get the incoming message as a byte array
var messageData = new byte[buffer.Count];
Array.Copy(buffer.Array, buffer.Offset, messageData, 0, messageData.Length);
bufferManager.ReturnBuffer(buffer.Array);
//Now convert it into a string for post-processing. Look at the content-type to determine which encoder to use.
string stringResult;
if (contentType != null && contentType.Contains("multipart/related"))
{
Message unprocessedMessageResult = this.mtomEncoder.ReadMessage(buffer, bufferManager, contentType);
stringResult = unprocessedMessageResult.ToString();
}
else {
//If it's not a multi-part message, the byte array already has the complete content, and it simply needs to be converted to a string
stringResult = Encoding.UTF8.GetString(messageData);
}
Message processedMessageResult = functionToDoPostProccessing(stringResult);
return processedMessageResult;
}