通过HTTPS使用WinHttp的SOAP请求返回错误代码415 [英] SOAP request using WinHttp over HTTPS returns error code 415

查看:849
本文介绍了通过HTTPS使用WinHttp的SOAP请求返回错误代码415的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要创建一个通过HTTPS连接到Web服务的SOAP客户端.我正在使用winhttp类来发送带有SOAP正文的HTTP请求,但是我从服务器获得的答复是HTTP 415->不支持的媒体类型.有人知道我在做什么错吗?

我对HTTP 415进行了一些研究.许多开发人员说Content-Type不正确.我试图改变这种情况,但是没有运气.

以下是我使用的代码.

我试图通过curl连接到此Web服务,但它给了我一个无效的证书,但我可以绕过curl进行操作-然后Web服务可以正常工作.

我试图绕过winhttp中的无效证书,但不确定是否正确执行了此操作.

预先感谢,
问候,

赫曼特
________________________________________________________

Hi,

I need to create a SOAP client that connects to a web service over HTTPS. I am using winhttp class to send the HTTP request with the SOAP body, but the reply I get from the server is HTTP 415 --> Unsupported media type. Does anyone know what I am doing wrong?

I have researched a bit on HTTP 415. Many developers say that the Content-Type is incorrect. I have tried to change this, with no luck.

Below is the code I use.

I have tried to connect to this webservice via curl, and it gives me an invalid certificate, but I can bypass this in curl - then the webservice works correctly.

I have tried to bypass the invalid certificate in winhttp, but not sure if I did this correctly.

Thanks in advance,
Regards,

Hemant
________________________________________________________

HINTERNET hinet, hconnection, hsession;

	LPCWSTR types[2];
	types[0] = L"text/*";
	types[1] = 0;

	hinet = WinHttpOpen(L"application",
			WINHTTP_ACCESS_TYPE_NO_PROXY,
			WINHTTP_NO_PROXY_NAME,
			WINHTTP_NO_PROXY_BYPASS, 
			0);


        hsession = WinHttpConnect(hinet,
                  L"test.myservice.com",
                  INTERNET_DEFAULT_HTTPS_PORT,
                  0);


hconnection = WinHttpOpenRequest(hsession,
                        L"POST",
                        L"/findme.asmx",
                        NULL,
                        L"https://test.myservice.com/findme.asmx",
                        types,
                        WINHTTP_FLAG_SECURE);

a = WinHttpSendRequest(hconnection,
		0,
		0,
		Content,
		((CString)Content).GetLength(),
		((CString)Content).GetLength(),
		0);
//Used to bypass cert.
DWORD	dwFlags = 
		SECURITY_FLAG_IGNORE_CERT_CN_INVALID
		| SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
		| SECURITY_FLAG_IGNORE_UNKNOWN_CA
		| SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;

a = WinHttpSetOption(
     hconnection ,
     WINHTTP_OPTION_SECURITY_FLAGS,
     &dwFlags,
     sizeof(DWORD));


	a = WinHttpReceiveResponse(hconnection,0);

	DWORD dwStatus = 0;				
	DWORD dwStatusSize = sizeof(DWORD);									DWORD dwIndex = 0;	

	a = WinHttpQueryHeaders(hconnection,
				WINHTTP_QUERY_FLAG_NUMBER |  WINHTTP_QUERY_STATUS_CODE,
				NULL,
				&dwStatus,
				&dwStatusSize,
				&dwIndex);

推荐答案

我向大家表示歉意,因为他们没有尽快回来.
无论如何,您知道错误415是媒体类型错误.相当模糊的错误描述.他们应该将其重命名为Content-Type错误,这是因为错误415与HTTP标头中的内容类型有关.
如果有人遇到此错误,则嗅探HTTP请求,他们将看到未设置内容类型.

最初,我认为内容类型将由以下请求(如下)设置,其中参数"types"为
.
I apologize to everyone for not getting back sooner.
Anyway, as you know error 415 is a media type error. Quite vague error description. They should rather rename it to Content-Type error, this is because error 415 related to the content type in the HTTP Header.
If any one who has this error, sniffs the HTTP Request, they will see the content type is not been set.

Initially, I thought the content type will be set by the following request (below), where the parameter ''types'' is.

hconnection = WinHttpOpenRequest(hsession,
   L"POST",
   L"/findme.asmx",
   NULL,
   L"https://test.myservice.com/findme.asmx",
   types,
   WINHTTP_FLAG_SECURE);



但是,事实并非如此.
必须使用WinHttpAddRequestHeaders函数设置Content-Type.
EG:



However this is not so.
The Content-Type must be set using the WinHttpAddRequestHeaders function.
EG:

temp = "Content-Type: text/xml; charset=utf-8";
    
Result = 
WinHttpAddRequestHeaders(hInternet,             //Request Handle
CT2W(temp.GetBuffer()),                         //Header
temp.GetLength(),                               //Header Length
WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE); 
//Flags --> Add or Replace



这应该解决错误415.



This should sort out error 415.


我找到了解决方案:).

有时间我会尽快发布解决方案.
I found the solution :).

I will post the solution as soon as I have time.


这篇关于通过HTTPS使用WinHttp的SOAP请求返回错误代码415的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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