InternetReadFile winnet API中的Unicode(阿拉伯字符)内容? [英] Unicode(arabic characters) content from InternetReadFile winnet API?

查看:114
本文介绍了InternetReadFile winnet API中的Unicode(阿拉伯字符)内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在消息框中显示URL(包含unicode字符)响应。



但是垃圾字符显示,所以我尝试进行不同的转换,但是alphabits只显示剩余的unicode字符显示为?(或)垃圾值。



即时通讯试用7天...... plz帮我...我很新的编码plz写完整的代码。

I have tried to display URL(contain unicode characters) response in Message Box.

But garbage characters displaying, so that i tried to made different conversions, but alphabits only displaying remaining unicode characters displaying as ?(or)garbage values.

im trying this from 7 days..plz help me...im very new to coding plz write the complete code.

HINTERNET hInternet = InternetOpen( _T(""), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );

HINTERNET hConnect = InternetConnect( hInternet, L"http://xxxxxxxxxx.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);

 DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD;

 HINTERNET hRequest  = InternetOpenUrl(hInternet,  L"http://xxxxxxxxxxx.com/c.php?varname=artxt&text=يبتىلمينبىمنيبغعاanand123",  NULL, 0, options, 0); 


TCHAR buffer[100];
DWORD bytesRead;
InternetReadFile(hRequest, buffer,100, &bytesRead);
MessageBoxW(NULL,ATL::CA2W(buffer),L"Check",MB_OK);
}
  
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);







when i added below code:







HttpQueryInfo (hRequest,HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) lpHeadersA,
&dwSize, NULL);
MessageBoxW(NULL,(LPWSTR)lpHeadersA,L"Type",MB_OK);







It giving output:







HTTP/1.1 200 OK
Date: Mon, 05 Nov 2012 03:55:10 GMT
Server: Apache
Keep-Alive: timeout=5, max=74
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

推荐答案

如果请求的网站上的文本使用UTF-8编码,则必须将其转换为在消息框中显示:



If the text from the requested web site uses UTF-8 encoding, it must be converted to show it in a mesage box:

// Use char here. InternetReadFile() reads bytes.
char buffer[100];
DWORD bytesRead;
InternetReadFile(hRequest, buffer, sizeof(buffer), &bytesRead);

// Get size of converted string including terminating NULL byte.
// With UTF-8 we may omit this and use bytesRead + 1 as size
//  because the resulting string would not have more than bytesRead wide
//  characters.
int nSize = ::MultiByteToWideChar(CP_UTF8, 0, buffer, bytesRead + 1, NULL, 0);
if (nSize)
{
    LPWSTR lpszText = new WCHAR[nSize];
    // Convert UTF-8 string to wide string
    ::MultiByteToWideChar(CP_UTF8, 0, buffer, bytesRead, lpszText, nSize);
    // Terminate string. buffer may be not NULL terminated.
    lpszText[nSize - 1] = L''\0'';
    MessageBoxW(NULL, lpszText, L"Check", MB_OK);
    delete [] lpszText;
}



如果页面使用其他编码,请传递此代替CP_UTF8(例如28596与ISO-8859-6,请参阅 Code_Page Identifiers [ ^ ]。


我对你发布的代码做了一些修改,我假设你的网页使用utf-8:



测试网页申请:



I made some modifications on the code you posted, I assumed that your web page uses utf-8:

Test web application:

#!python
# -*- coding: utf-8 -*-
from bottle import *

content_type = 'text/html; charset=utf-8'

@route("/ar/")
def ar():
    response.content_type = content_type
    return "تجربة"

@route("/en/")
def en():
    response.content_type = content_type
    return "Test"

@route("/zn/")
def zn():
    response.content_type = content_type
    return "测试"

run(port=8080)





代码:





The code:

#include <windows.h>
#include <wininet.h>

#pragma comment(lib, "wininet.lib")

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    HINTERNET hInternet = InternetOpen(TEXT("foo"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

    HINTERNET hConnect = InternetConnect(hInternet, TEXT("http://localhost:8080/"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);

    DWORD options = INTERNET_OPTION_HTTP_DECODING | INTERNET_FLAG_NEED_FILE | INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_RESYNCHRONIZE | INTERNET_FLAG_RELOAD;

    HINTERNET hRequest  = InternetOpenUrl(hInternet,  TEXT("http://localhost:8080/ar/"), NULL, 0, options, 0); 

    BYTE buffer[100] = {0};
    TCHAR szResp[100] = {0};
    DWORD bytesRead;

    InternetReadFile(hRequest, &buffer[0], sizeof(buffer) - 1, &bytesRead);

    MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCSTR) &buffer[0], sizeof(buffer), &szResp[0], sizeof(szResp));

    MessageBox(NULL, &szResp[0], TEXT("Check"), MB_OK);

    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);

	return 0;
}









注意,请确保处理错误。





Note, make sure you handle the errors.


只需在您的语句中添加以下语句标题



just add the below statement in your header

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">





如果你用c ++阅读这个页面,那么首先阅读标题,即http标题,它将告诉你需要遵循的编码,然后简单地将整个页面转换为unicode(宽字符)。你很容易操纵文字。否则你将不得不遵循特殊的方法来操纵。



and if you are reading this page with your c++ then first read the header i.e. http header, it will tell you what encoding you need to follow then simply convert entire page to unicode(wide character). it will be easy for you to manipulate the text. otherwise you will have to follow special method to manipulate.


这篇关于InternetReadFile winnet API中的Unicode(阿拉伯字符)内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆