gpt4 book ai didi

windows - wininet API 的 P/Invoke 机制

转载 作者:可可西里 更新时间:2023-11-01 11:40:35 29 4
gpt4 key购买 nike

我正在使用 wininet api 将 HTTP POST 发送到 SSL 服务器。我想使用 P/Invoke 将这些 api 包装到 C#,因为 C# 中的原始代码。我如何将这些 api 包装到 c#。我试过了,但没有用。我正在为 Windows Mobile 和智能手机做这个。

LPCTSTR lpszAgent = _T("CeHttp");
DWORD dwError;
DWORD sizeInResult, sizeOutResult, sizeToWrite, sizeWritten,dwRead;
HINTERNET hInternet=NULL;
HINTERNET hConnect=NULL;
HINTERNET hRequest=NULL;
LPDWORD pSizeInResult = &sizeInResult;

LPDWORD pSizeOutResult = &sizeOutResult;
LPDWORD pSizeToWrite = &sizeToWrite;
LPDWORD pSizeWritten = &sizeWritten;

hInternet=InternetOpen(lpszAgent,INTERNET_OPEN_TYPE_PRECONFIG,NULL,INTERNET_INVALID_PORT_NUMBER,0);
if(!hInternet)
{

dwError = GetLastError();

}

LPCTSTR lpszServerName = _T("mundusync");
INTERNET_PORT nServerPort =444;
LPCTSTR lpszUserName = L"";
LPCTSTR lpszPassword = L"";
DWORD dwConnectFlags = 0;
DWORD dwConnectContext = 0;
DWORD dwCode = NULL;
DWORD dwSize = 8000;
char charBuffer[1000];
TCHAR szBuffer[1000];


int read = 0;
char* str = "*/*", buff[1024] = {};



hConnect = InternetConnect(hInternet,L"s-syncml.geodesic.net",
nServerPort,
L"",
L"",
INTERNET_SERVICE_HTTP,
0,
0);




if(! hConnect)
{


dwError = GetLastError();

}


LPCWSTR lpszVerb = _T("POST");
LPCWSTR lpszObject = L"/";
LPCWSTR lpszVersion = NULL;
LPCWSTR lpszReferer = NULL;
LPCWSTR *lplpszAcceptTypes = NULL;

DWORD dwFlags = INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID|
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;

DWORD dwOpenRequestContext = 0;

hRequest=HttpOpenRequest(hConnect,L"POST",L"/",HTTP_VERSION,L"",lplpszAcceptTypes,dwFlags,dwOpenRequestContext);


if(!hRequest )

{

dwError = GetLastError();

}




char lpPostData[637] ="<SyncML> some post data </SyncML>";

LPCWSTR lpszHeaders =_T("Content-Type: application/vnd.xyz+xml");
BOOL bResult;
if(!HttpSendRequest(hRequest,lpszHeaders,wcslen(lpszHeaders),lpPostData,strlen(lpPostData)))
{

dwError = GetLastError();

}

if (!(InternetQueryDataAvailable (hRequest, pSizeInResult, 0, 0)))
{
//wsprintf (dwError, TEXT("%s: %x"), TEXT("InternetQueryDataAvailableError"), GetLastError());
return;
//WriteFile (hFile, szErrMsg, sizeToWrite, pSizeWritten, NULL);
//goto error;
}

if (sizeInResult >= 1024)
{
int num = sizeInResult / 1024;

do
{
//InternetReadFile (hRequest, lpBuffer, 1024, pSizeOutResult);
// WriteFile (hFile, lpBuffer, sizeOutResult, pSizeWritten, NULL);
}
while (--num > 0);
}
else
{
//InternetReadFile (hRequest, lpBuffer, sizeInResult, pSizeOutResult);
// WriteFile (hFile, lpBuffer, sizeOutResult, pSizeWritten, NULL);

do
{
// read from Internet HTTP server
if(InternetReadFile(hRequest, charBuffer,
1000, &dwRead))
{
// convert to Unicode and display
charBuffer[dwRead] = '\0';
mbstowcs(szBuffer, charBuffer, dwRead);
//cout « szBuffer;
szBuffer[dwRead] = '\0';
}


} while(dwRead > 0);

}

if(!hInternet)
InternetCloseHandle(hInternet);
if(!hConnect)
InternetCloseHandle(hConnect);
if(!hRequest)
InternetCloseHandle(hRequest);

最佳答案

你应该检查 P/Invoke website .它们列出了众所周知的 DLL 的所有 P/Invoke 定义,尤其是“wininet”。您还可以找到示例代码。

关于windows - wininet API 的 P/Invoke 机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1872733/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com