mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
GBK encoding support for http (#3165)
* http response add GBK charset support * default encode do not change * change compare response encode `==` to `===`
This commit is contained in:
@ -66,7 +66,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
var pair: org.nativescript.widgets.Async.Http.KeyValuePair;
|
||||
for (i = 0; i < length; i++) {
|
||||
pair = jHeaders.get(i);
|
||||
|
||||
|
||||
(<any>http).addHeader(headers, pair.key, pair.value);
|
||||
}
|
||||
}
|
||||
@ -74,16 +74,28 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
callbacks.resolveCallback({
|
||||
content: {
|
||||
raw: result.raw,
|
||||
toString: () => {
|
||||
if (types.isString(result.responseAsString)) {
|
||||
return result.responseAsString;
|
||||
toString: (encoding?: http.HttpResponseEncoding) => {
|
||||
let str: string;
|
||||
if (encoding) {
|
||||
str = decodeResponse(result.raw, encoding);
|
||||
} else {
|
||||
str = result.responseAsString;
|
||||
}
|
||||
if (types.isString(str)) {
|
||||
return str;
|
||||
} else {
|
||||
throw new Error("Response content may not be converted to string");
|
||||
}
|
||||
},
|
||||
toJSON: () => {
|
||||
toJSON: (encoding?: http.HttpResponseEncoding) => {
|
||||
ensureUtils();
|
||||
return utils.parseJSON(result.responseAsString);
|
||||
let str: string;
|
||||
if (encoding) {
|
||||
str = decodeResponse(result.raw, encoding);
|
||||
} else {
|
||||
str = result.responseAsString;
|
||||
}
|
||||
return utils.parseJSON(str);
|
||||
},
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
@ -195,3 +207,11 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function decodeResponse(raw: any, encoding?: http.HttpResponseEncoding) {
|
||||
let charsetName = "UTF-8";
|
||||
if (encoding === http.HttpResponseEncoding.GBK) {
|
||||
charsetName = 'GBK';
|
||||
}
|
||||
return raw.toString(charsetName)
|
||||
}
|
||||
|
Reference in New Issue
Block a user