Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
________________________________________________________

C++
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);
Posted
Updated 13-Jun-10 8:13am
v3
Comments
Deepen_3k 10-Dec-10 2:57am    
can you please post your solution

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.

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


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

VB
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


This should sort out error 415.
 
Share this answer
 
Comments
Deepen_3k 17-Dec-10 9:29am    
WinHttpAddRequestHeaders made it work
I found the solution :).

I will post the solution as soon as I have time.
 
Share this answer
 
Comments
Deepen_3k 9-Dec-10 8:53am    
What is the solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900