How to get Cyrillic responsetext - vb6

Dim http As WinHttpRequest
Set http = New WinHttpRequest
http.open "POST", "test.php", False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.send "txtmbx=test"
msgbox(http.responsetext)
http.responsetext is in Cyrillic and I'm unable to read text. How can I retrieve Cyrillic?

The WinHTTPRequest does not support an encoding method, and while it doesn't explicitly say, I expect it uses CP_ACP, the system default codepage when converting from the received byte data to a string.
You can use the ResponseBody method to get the data as a byte array and use StrConv to convert to a string as you wish.

Related

Apache HTTP Client forcing UTF-8 encoding

I'm making a rest call using the org.apache.http package as below. I'm expecting user profile details in the response in English and other international languages.
HttpGet req = new HttpGet(baseUrl + uri);
HttpResponse res= closeableHttpClient.execute(req);
The response has UTF-8 as character set, which is what I wanted. From here, I used 2 approaches to unmarshall the response to a map.
Approach-1:
String response = EntityUtils.toString(res.getEntity(),"UTF-8");
// String response = EntityUtils.toString(httpResponse.getEntity(),Charset.forName("UTF-8"));
map = jsonConversionUtil.convertStringtoMap(response);
Issue:
httpResponse.getEntity() was returning StringEntity object which had default charset as ISO_8859_1, but even when I force to convert to UTF-8 (uncommmented line and commented line above, both I tried), I'm not able to override to UTF-8.
Approach-2:
HttpEntity responseEntity = res.getEntity();
if (responseEntity != null ) {
InputStream contentStream = responseEntity.getContent();
if (contentStream != null) {
String response = IOUtils.toString(contentStream, "UTF-8");
map = jsonConversionUtil.convertStringtoMap(response);
}
}
Issue:
IOUtils.toString(contentStream, "UTF-8"); is not setting to UT8.
I am using httpclient 4.3.2 jar & httpcore-4.3.1 jar. Java version used in Java 6. I can't upgrade to a higher java version.
Can you please guide how I can set to UTF-8 format.
If the StringEntity object has an ISO-8859-1 encoding, then the server has returned its response encoded as ISO-8859-1. Your assumption that "the response has UTF-8 as character set" is most likely wrong.
Since it's ISO-8859-1, both your approaches don't work:
Approach 1: The "UTF-8" parameter has no effect as the parameter specifies the default encoding in case the server doesn't specify one (see EntityUtils.toString(). But the server has obviously specified one.
Approach 2: Reading the binary content as UTF-8, which is in fact encoded in ISO-8859-1, will likely result in garbage (though many characters have a similar representation in UTF-8 and ISO-8859-1).
So try to ask the server to return UTF-8:
HttpGet req = new HttpGet(baseUrl + uri);
req.addHeader("Accept", "application/json");
req.addHeader("Accept-Charset", "utf-8");
HttpResponse res = closeableHttpClient.execute(req);
If it disregards the specified characters set and still returns JSON in ISO-8859-1, then it will be unable to use characters outside the ISO-8859-1 range (unless it uses escaping within JSON).

Passing a subscription key as a request header with msxml2.ServerXMLHTTP - Classic ASP/VB

I'm trying to pull data from an NHS API using a little bit of classic ASP (all I know I'm afraid) but am struggling to successfully pass the subscription key to the API.
The instructions are as follows:
Pick a page on the NHS website, for example: https://www.nhs.uk/conditions/acne.
Make a note of the path, for example: conditions/acne.
Using a tool such as curl, Postman or your web browser, make a GET request to https://api.nhs.uk/content/acne with a valid subscription key subscription‑key: {subscription-key} in the request header.
You’ll receive a JSON response structured using schema.org and the fields for this are explained in the following documentation....
From https://developer.api.nhs.uk/documentation/content-api
So, I wrote the following...
<%
Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
xml.Open "GET", "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/", False
on error resume next
xml.setRequestHeader "subscription‑key", "MY-API-KEY-HERE"
xml.setRequestHeader "Content-Type", "application/json"
xml.setRequestHeader "Accept", "application/json"
xml.Send
Response.Write "<h1>The HTML text</h1><xmp>"
Response.Write xml.responseText
Set xml = Nothing
%>
This just gives me the following response:
{ "statusCode": 401, "message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API." }
They have example scripts in 5 different languages but not ASP or even ASP.NET
Any ideas what I can try to get this working?
Thanks
EDIT
Trying the method suggested here How can I post data using cURL in asp classic? ...
<%
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim url: url = "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/"
'Dim data: data = "something=this" - took this out as its a querystring for POST
With http
Call .Open("GET", url, False)
'Call .SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")
'Call .Send(data) <- the data was the querystring, so not relevant here
Call .Send()
End With
If Left(http.Status, 1) = 2 Then
'Request succeeded with a HTTP 2xx response, do something...
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
%>
This gives me Invalid procedure call or argument: 'SetRequestHeader'
EDIT WITH SOLUTION
Working code with hyphen issue fixed...
<%
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim url: url = "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/"
With http
Call .Open("GET", url, False)
Call .SetRequestHeader("subscription-key", "MYKEYHERE")
Call .Send()
End With
If Left(http.Status, 1) = 2 Then
'Request succeeded with a HTTP 2xx response, do something...
Response.Write http.responseText
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
%>
Thanks Lankymart!
Tried your take on the duplicate example and it returned
Invalid procedure call or argument: 'SetRequestHeader'
This puzzled me as that code had been tested before and work fine so what changed?
So I dug into the SetRequestHeader method calls.
Turns out the error only occurs on this line;
Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")
In the end, removed subscription‑ from the header name and it worked without causing a compilation error.
That led me to check the hyphen in the code using Asc("‑") and comparing that with a standard hyphen and sure enough they are different.
<%
Response.Write Asc("‑") & "<br />" 'From the code
Response.Write Asc("-") & "<br />" 'Standard hyphen
%>
Output:
-15454
45
Replaced the character with a standard hyphen the error has gone and the code runs returning;
Server returned: 401 Unauthorized

MSXML2.ServerXMLHTTP and national characters

This question is related to this one: Character encoding Microsoft.XmlHttp in Vbscript, but differs in one thing, the national characters are in the domain name, not only arguments.
The task is: download a page from the given URL.
I already solved problem of passing UTF8 string into VBScript by reading it from UTF8 encoded file through ADO.
But now when I try opening it MSXML2.ServerXMLHTTP returns error: The URL is invalid.
Here is VBScript code:
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("fileWithURL.txt")
url = objStream.ReadText()
objStream.Close
Set XMLHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
XMLHttpReq.Open "GET", url, False
XMLHttpReq.send
WEBPAGE = XMLHttpReq.responseText
If you put something like hxxp://россия.рф/main/page5.html into the UTF8 encoded fileWithURL.txt the script will raise an error while working ok with hxxp://google.com.
The workaround is to use ascii representation of the domain name - but I yet haven't found PunnyCode encoder for vbscript (apart from Chillkat which is an overkill for my task).
Will appreciate your help on the main problem or workaround.
I've made an amazing journey in to depth of my hard drive and found a code writen by / for Jesper Høy. This was the source code of SimpleDNS Plus' IDN Conversion Tool at that time.
Archive.org page snapshot: http://www.simpledns.com/idn-convert.asp
Archive.org file snapshot: idn-convert-asp.zip
You can also copy the whole code from this gist.
Create a function to convert URLs.
Function DummyPuny(ByVal url)
Dim rSegments : rSegments = Split(url, "/")
If UBound(rSegments) > 1 Then
rSegments(2) = DomainPunyEncode(rSegments(2))
End If
DummyPuny = Join(rSegments, "/")
End Function
Then convert your url before making the request.
XMLHttpReq.Open "GET", DummyPuny(url), False

ruby and net/http request without content-type

I'm trying to make a call to a Tika server using Net::HTTP::Put. The issue is that the call always passes the Content-Type, which keeps Tika from running the detectors (which I want) and then chokes due to the default Content-Type of application/x-www-form-urlencoded. Tika docs suggest to not use that.
So, I have the following:
require 'net/http'
port = 9998
host = "localhost"
path = "/meta"
req = Net::HTTP::Put.new(path)
req.body_stream = File.open(file_name)
req['Transfer-Encoding'] = 'chunked'
req['Accept'] = 'application/json'
response = Net::HTTP.new(host, port).start { |http|
http.request(req)
}
I tried adding req.delete('content-type') and setting initheaders = {} in various ways, but the default content-type keeps getting sent.
Any insights would be greatly appreciated, since I would rather avoid having to make multiple curl calls ... is there any way to suppress the sending of that default header?
If you set req['Content-Type'] = nil then Net::HTTP will set it to the default of 'application/x-www-form-urlencoded', but if you set it to a blank string Net::HTTP leaves it alone:
req['Content-Type'] = ''
Tika should see that as an invalid type and enable the detectors.
It seems that Tika will run the detectors if the Content-Type is application/octet-stream. Adding
req.content_type = "application/octet-stream"
is now allowing me to get results.

Retrieving data from httprequest

I'm working on this server side asp script.It is supposed to receive a xml stream from a web app and save it in a xml file. The problem is I'm haven't been able to read this stream. I used different methods and I can't seem to find the right one. One other thing is for testing I'm using rest console add on for chrome and it seems to work with no problems but when my client sends the stream I'm unable to read it and they receive a code 500 error.
at first I tried reading the stream in binary mode then converting it
function readContent()
dim a,b
a=Request.TotalBytes
b=Request.BinaryRead(a)
writeInLogFile(" ")
writeInLogFile(Time & Request.ServerVariables("ALL_RAW"))
writeInLogFile(Time & " Data read in binary mode with a size of " & a)
readContent = URLDecode(BytesToStr(b))
writeInLogFile(Time & " the length of the converted string is : "& len(readContent))
end function
but here's what I keep getting on my log file
17:12:10Content-Length: 8416
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Authorization: Basic
Host:
User-Agent: Jakarta Commons-HttpClient/2.0.2
17:12:10 Data read in binary mode with a size of 8416
17:12:10 Converting binary to string
and then it crashes when I try to write the converted string
then I switched to request.form
function readContent()
'writeInLogFile(Time & " " & URLDecode(Request.Form))
writeInLogFile(Time & Request.ServerVariables("ALL_RAW"))
readContent = URLDecode(Request.Form)
writeInLogFile(Time & " the length of the converted string is : "& len(readContent))
end function
but yet again when testing via rest console all is working and when actually receiving the stream from my client it just crashes.
Anyone faced a similar problem, or has an idea to how can I solve this thing
thanks in advance
update :
here is the decoding function
FUNCTION URLDecode(str)
'// This function:
'// - decodes any utf-8 encoded characters into unicode characters eg. (%C3%A5 = å)
'// - replaces any plus sign separators with a space character
'//
'// IMPORTANT:
'// Your webpage must use the UTF-8 character set. Easiest method is to use this META tag:
'// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
'//
Dim objScript
Set objScript = Server.CreateObject("ScriptControl")
objScript.Language = "JavaScript"
URLDecode = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))")
Set objScript = NOTHING
'writeInLogFile(Time & " the length of the converted string is : "& len(URLDecode))
END FUNCTION
The encoding that client said would be sent was not the same as actually sent by the stream.
Request.Form is the answer.

Resources