I am currently trying to get a correct VB.NET (or C#) code fragment for doing a LOGIN on astrometry. The procedure is described here: and .html
- I tried a lot of things, but the best I get is {"status": "error", "errormessage": "no json"}
- If I press the button on the web page with a wrong API key the site response is: {"status": "error", "errormessage": "bad apikey"}. Getting this in my code would already be enough.
This is my current fragment:
Dim LoginURL As String = ";
Dim Enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim MediaType As String = "application/json"
Dim JSONContent As String = "{""apikey"": """ & APIKey & """}"
Dim C2String As New System.Net.Http.StringContent(JSONContent , Enc, MediaType)
'Run the query
Dim MyClient As New System.Net.Http.HttpClient
Dim F As System.Net.Http.HttpResponseMessage = Await MyClient.PostAsync(LoginURL, C2String)
Dim Code As String = Await F.Content.ReadAsStringAsync()
I think I miss some parameter to indicate that the query is in JSON format, but I have no idea where.
Any hints?
Best regards Martin
I am currently trying to get a correct VB.NET (or C#) code fragment for doing a LOGIN on astrometry. The procedure is described here: https://nova.astrometry/api_help and https://astrometry/doc/net/api.html
- I tried a lot of things, but the best I get is {"status": "error", "errormessage": "no json"}
- If I press the button on the web page with a wrong API key the site response is: {"status": "error", "errormessage": "bad apikey"}. Getting this in my code would already be enough.
This is my current fragment:
Dim LoginURL As String = "http://nova.astrometry/api/login"
Dim Enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim MediaType As String = "application/json"
Dim JSONContent As String = "{""apikey"": """ & APIKey & """}"
Dim C2String As New System.Net.Http.StringContent(JSONContent , Enc, MediaType)
'Run the query
Dim MyClient As New System.Net.Http.HttpClient
Dim F As System.Net.Http.HttpResponseMessage = Await MyClient.PostAsync(LoginURL, C2String)
Dim Code As String = Await F.Content.ReadAsStringAsync()
I think I miss some parameter to indicate that the query is in JSON format, but I have no idea where.
Any hints?
Best regards Martin
Share Improve this question edited Mar 30 at 20:16 It all makes cents 5,0633 gold badges15 silver badges34 bronze badges asked Mar 30 at 17:25 Martin WeissMartin Weiss 112 bronze badges 1- The following may be of interest: Make HTTP requests with the HttpClient class – It all makes cents Commented Mar 30 at 18:22
1 Answer
Reset to default 1You need to make a few changes to make this work.
First, encode the value of the JSONContent
variable by using the System.Web.HttpUtility
class:
Dim JSONContent As String = System.Web.HttpUtility.UrlEncode("{""apikey"": """ & APIKey & """}")
Then prepend the request-json=
string as noted in the documentation:
JSONContent = "request-json=" & JSONContent
Lastly, set the correct content type on the StringContent
object:
Dim C2String As New System.Net.Http.StringContent(JSONContent , Enc, "application/x-www-form-urlencoded")
I tested this myself and it works. Please check the VB.NET syntax. I am a C# developer so I did my best to properly translate it.