WinHttpSendRequest 返回 ERROR_INVALID_PARAMETER [英] WinHttpSendRequest returns ERROR_INVALID_PARAMETER

查看:33
本文介绍了WinHttpSendRequest 返回 ERROR_INVALID_PARAMETER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 betfair API 与 windows API 一起使用.>

我在尝试使用以下代码登录时卡住了(用户名和密码已更改).

char *headers = "X-Application: MakJhSABCDq8sbPIr\r\nAccept: application/json";char *usernameandpassword = "username=mick&password=xyz123";WinHttpSendRequest(请求,(LPCWSTR) 标头,-1,用户名和密码,strlen(用户名和密码),strlen(用户名和密码),0);

但调用返回的值为 ERROR_INVALID_PARAMETER.但我不知道哪个参数不好.

解决方案

LPCWSTR 作为 const wchar_t* 的 typedef.在 Windows 上,它使用 UTF-16 编码.基本上,您将 8 位 ASCII 字符串 (headers) 类型转换为 16 位宽的字符串,并告诉 WinHttpSendRequest() 计算字符串长度,因为它是不是有效的宽字符串.

对此的解决方案是首先使用适当的宽字符串:

const wchar_t *headers = L"X-Application: MakJhSABCDq8sbPIr\r\nAccept: application/json";

无论如何,用 char* 指向字符串文字是错误的,因为字符串文字是不可修改的,修改它们是未定义的行为.它应该总是 const.

I am attempting to get the betfair API working with windows API.

I am stuck attempting to login with the following code (username and passwords have been changed).

char *headers = "X-Application: MakJhSABCDq8sbPIr\r\nAccept: application/json";

char *usernameandpassword = "username=mick&password=xyz123";

WinHttpSendRequest(
                    hrequest,
                    (LPCWSTR)headers,
                    -1,
                    usernameandpassword,
                    strlen(usernameandpassword),
                    strlen(usernameandpassword),
                    0);

but the call is returning with the value ERROR_INVALID_PARAMETER. But I have no idea which parameter is bad.

解决方案

LPCWSTR as a typedef for const wchar_t*. On windows, that uses UTF-16 encoding. Basically, you are type-casting an 8bit ASCII string (headers) to a 16bit wide string, and telling WinHttpSendRequest() to calculate the string length, which fails since it is not a valid wide string.

A solution to this is to use a proper wide string for the first place:

const wchar_t *headers = L"X-Application: MakJhSABCDq8sbPIr\r\nAccept: application/json";

Anyway, pointing to a string literal with a char* is wrong, since string literals are not modifiable, modifying them is undefined behavior. It should always be const.

这篇关于WinHttpSendRequest 返回 ERROR_INVALID_PARAMETER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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