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.
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
How would I send a raw POST request in Visual Basic .NET (2010)? When I say raw, I mean without using System.Net.WebRequest to form one, but rather by forming your own headers and sending them with the StreamWriter..
I think I know how to do it with a GET request, but I'm not sure how I would send a POST request.
...
Example GET request (NOTE THAT HERE I USE WEBREQUEST, WHICH I DON'T WANT TO):
Private Function HTTPGet(ByVal URL As String) As String
On Error GoTo fail
Dim Output As String = String.Empty
Dim Request As WebRequest = WebRequest.Create(URL)
Request.Method = "GET"
Using Response As WebResponse = Request.GetResponse
Using Stream As Stream = Response.GetResponseStream
Output = (New StreamReader(Stream)).ReadToEnd
End Using
End Using
Return Output
fail:
Return Nothing
End Function
If you really want to manually create these POST's, you will have to do a little research on HTTP requests and sockets. Sockets will create connections to a destination service and let you determine what data you want to send. To build the data properly, you will need to research what the format will need to be for an HTTP POST.
Check out this link to learn more about HTTP GET/POST. Scroll down to (The POST Method) to see a sample HTTP POST.
http://www.jmarshall.com/easy/http/
Here is a link on sockets:
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
I need to know which encoding type to use for retrieving Japanese characters from a web page.
I have used following encoding for my web request(httpRequest). But, It's not working.
Dim receiveStream As Stream = hwresponse1.GetResponseStream()
Dim readStream As New StreamReader(receiveStream, System.Text.Encoding.GetEncoding("ISO-8859-1"))
I appreciate any help. Thanks in advance!
The HttpWebResponse object contains a property CharacterSet that will tell you what character set to use
Please see http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.characterset.aspx for more details
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 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