|
-
Jun 16th, 2013, 10:43 PM
#1
Thread Starter
New Member
httpclient help and utf-8
After many years I have returned my hand at some programming... daunting....
Using Visual Studio 2012 and trying to write a vb application to run on windows phone WP8 - as I want to move off the iPhone back to WP but there is one program that currently does not exist for WP that I can't live without - so I might as well write it myself...
I need to interface to my sinology nas, and it has an API which I am following... it tells me that the first call to the api should be something like this;
http://myds.com:5000/webapi/query.cg...hod=query&quer y=SYNO.API.Auth,SYNO.DownloadStation.Task
-
Jun 17th, 2013, 06:08 AM
#2
Re: httpclient help and utf-8
-
Jun 17th, 2013, 06:49 AM
#3
Thread Starter
New Member
Re: httpclient help and utf-8
I was using my surface typing this out, and accidentally hit enter and the question was submitted without my meaning to do so... Being a new member, the posting was not made immediately and had to sit and wait for a moderator to release it... So I was unable to go back and edit it... So I submitted the question again, but I guess the moderator saw the duplicate and did not let it go through...
So apologies - not a good start - but lets try again.....
So I am wanting to write a WP8 app for my phone that interfaces to my Synology NAS. Looking at the Synology API, I should check the version of the API before I do anything, and the documentation says I should do a GET as follows;
http://myds.com:5000/webapi/query.cg...n.TaskResponse
I have tried this in a browser, and it works well - returning what I had expected;
{"data":{"SYNO.API.Auth":{"maxVersion":1,"minVersion":1,"path":"DownloadStation/auth.cgi"}},"success":true}
The sample code I quickly put together (and mind you google search and msdn was not too helpful - searching for vb help brings stuff back from 2002, across many different platforms and found it hard to find something specific for visual studio 2012 and wp8);
Try
Dim response = Await New HttpClient().GetAsync("http://myds.com:5000/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth,SYNO.DownloadStation.TaskResp onse")
Dim result As String = Await response.Content.ReadAsStreamAsync()
MessageBox.Show(:got it")
MessageBox.Show(result)
Catch ex As Exception
MessageBox.Show("Exception")
MessageBox.Show(ex.ToString())
End Try
So it runs - sends the request, and then on the response causes an exception;
System.InvalidOperationException: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set ---> System.ArgumentException "UTF-8" is not a supported encoding name at
System.GlobalizationEncodingTable.internalGetCodePageFromName(String name) at
SystemGlobalization.EncodingTable.GetCodePageFromName(String name) at
System.Text.Encoding.GetEncoding(String name) at
System.Net.HttpContent <>c_DisplayClass1<ReadAsStringAsync>b_0(Task task)
--- End of inner exception stack trace ----
at System.RuntimeCompileServices.TaskAwaiter.ThrowForNonSuccess(task task)
at System.RuntimeCompilerServices.TaskAwaiter.HandkeNonSuccessAndDebuggerNotification(Task task)
at Systen.RuntimeCompilerServices.TaskAwaiter'1.GetResult()
at MiniBrowser2.MainPage.VB$StateMachine_0_bkmark_ClickMoveNext()
If I change the URI to something more common like - http://www.microsoft.com it will work and I get the response back from the server / html page - Which I can see and interrogate ?
So My question is HELP - am I supposed to be setting a header or something to tell it what character set to use ? I have tried changing various things in headers without any help so far...
Is it reading stream that is the problem and I should be using another method ???
If someone could point me in the right direction - preferably showing me the code to resurrect from the above - it would be greatly appreciated... 2 days off and on with google and I am frustrated for not finding an answer
Thanks In Advance
-
Jun 17th, 2013, 10:56 PM
#4
Thread Starter
New Member
Re: httpclient help and utf-8
Looks like I have found the problem... BUG.
http://connect.microsoft.com/VisualS...eaders#details
Indicates that Microsoft correctly coded this ReadAsStringAsync() correctly to the standards.... it is expecting and awaiting to receive UTF-8 in uppercase. However if it receives utf-8 in lowercase then an exception occurs.... Well according to the post above, and my limited testing...
Unfortunately I cant easily get synology to do the right thing... so I either need to wait months for a fix - or find another way to code it...
The post above suggests the only work around is to use GetByteArrayAsync() So far not successful in getting this to work - as all the examples use as task, and that does not seem to like my coding skills...
Any examples around I could try ?
Or another simple method that may work ?
-
Jun 18th, 2013, 09:07 PM
#5
Thread Starter
New Member
Re: httpclient help and utf-8
Finally got it to work - several days and a hundred google searches later.....
MessageBox.Show("about to send request")
Try
Dim client = Await New HttpClient().GetAsync("http://mydns:5000/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth,SYNO.DownloadStation.Task")
Dim bytes As Byte() = Await client.Content.ReadAsByteArrayAsync()
Dim result As String
MessageBox.Show("got it")
result = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1)
MessageBox.Show(result)
Catch ex As Exception
MessageBox.Show("exception")
MessageBox.Show(ex.ToString())
End Try
Does the job - maybe not eloquent - but works....
Now I can move on to cookies and authentication and some actual logic instead of stumbling around for code examples trying to work out if this was a relevant example of .net or vb4 or some other rubbish...
-
Jun 18th, 2013, 09:20 PM
#6
Re: httpclient help and utf-8
The 'Await' keyword is very new, introduced in VB 2012, which is why there's not so much information around about its use and many have little to no experience using it, myself included.
-
Jun 20th, 2013, 08:01 PM
#7
Thread Starter
New Member
Re: httpclient help and utf-8
Given in - somewhat....
After another couple of days of playing around - I cant get the authorisation to work properly... Always struggle to get the cookie from the server when using async httpclient... So given in...
Decided I will cheat - now I am just going to use a webbrowser control, set the url's I need, let the webbrowser handle all the cookie stuff behind the scene, and I will just interrogate the html returned. Make the control invisible and hopefully I can move on to logic and screen design instead of just playing around in the comms stuff - that will be a nice change...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|