mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +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:
@ -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)
|
||||
}
|
||||
|
@ -89,9 +89,9 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toString: () => { return NSDataToString(data); },
|
||||
toJSON: () => {
|
||||
return utils.parseJSON(NSDataToString(data));
|
||||
toString: (encoding?: http.HttpResponseEncoding) => { return NSDataToString(data, encoding); },
|
||||
toJSON: (encoding?: http.HttpResponseEncoding) => {
|
||||
return utils.parseJSON(NSDataToString(data, encoding));
|
||||
},
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
@ -125,7 +125,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
}
|
||||
});
|
||||
|
||||
if(options.url && debugRequest) {
|
||||
if (options.url && debugRequest) {
|
||||
var request = {
|
||||
url: options.url,
|
||||
method: "GET",
|
||||
@ -141,6 +141,10 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
});
|
||||
}
|
||||
|
||||
function NSDataToString(data: any): string {
|
||||
return NSString.alloc().initWithDataEncoding(data, 4).toString();
|
||||
function NSDataToString(data: any, encoding?: http.HttpResponseEncoding): string {
|
||||
let code = 4; //UTF8
|
||||
if (encoding === http.HttpResponseEncoding.GBK) {
|
||||
code = 1586;
|
||||
}
|
||||
return NSString.alloc().initWithDataEncoding(data, code).toString();
|
||||
}
|
||||
|
8
tns-core-modules/http/http.d.ts
vendored
8
tns-core-modules/http/http.d.ts
vendored
@ -113,6 +113,10 @@ declare module "http" {
|
||||
|
||||
export type Headers = { [key: string]: string | string[] };
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
/**
|
||||
* Encapsulates the content of an HttpResponse.
|
||||
*/
|
||||
@ -125,12 +129,12 @@ declare module "http" {
|
||||
/**
|
||||
* Gets the response body as string.
|
||||
*/
|
||||
toString: () => string;
|
||||
toString: (encoding?: HttpResponseEncoding) => string;
|
||||
|
||||
/**
|
||||
* Gets the response body as JSON object.
|
||||
*/
|
||||
toJSON: () => any;
|
||||
toJSON: (encoding?: HttpResponseEncoding) => any;
|
||||
|
||||
/**
|
||||
* Gets the response body as ImageSource.
|
||||
|
Reference in New Issue
Block a user