如何在C ++中使用通用的http / https代理? [英] How to use general http/https proxy with C++?

查看:354
本文介绍了如何在C ++中使用通用的http / https代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用http / https代理构建一个简单的c ++程序。我尝试使用MFC CInternetSession类。但它并不适用于许多代理服务器。所以我尝试使用libcurl。似乎运作良好。但我很累。当我尝试将表单发布到网站时。我在智能嗅探器中看到了如下情况。



[2016-01-13 1:49:16:973]

CONNECT m .pmang.com:443 HTTP / 1.1

用户代理:Mozilla / 4.0(兼容; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)

主持人:m.pmang.com

代理连接:保持活力



[2016-01-13 1:49:17:285]

HTTP / 1.1 200建立连接



[2016-01-13 1:49:17: 300]

00000000 16 03 01 00 EA 01 00 00 E6 03 01 40 D2 75 C5 FF ........ ... @。u ..



我的代码也收到了建立的HTTP / 1.1 200连接它是什么?我该怎么做才能解决它?Http / Https代理库在一般代理服务器上工作的地方(50.195.87.31) :3128)?



我发布了表单内容。所以我必须从远程服务器接收响应html。例如< html> XXX< / html>。但我的代码收到了HTTP / 1.1 200连接建立代理服务器。



我现在使用libcurl例如我应该如何用libcurl编写代码?

解决方案

< blockquote> CURLOPT_WRITEFUNCTION 是你的朋友。只要在执行操作后从服务器获得响应,就会调用在设置此选项时为libcurl提供的函数。


HTTP / 1.1 200建立连接是初始响应行(状态行)从服务器到您的请求。根据请求,它后面是标题行和消息正文(您感兴趣的)。您可以解析这些信息以了解连接是否成功,并解析标题行以了解正文编码。



您可以查看此链接:< a href =https://www.jmarshall.com/easy/http/> HTTP非常简单 [ ^ ]。



使用 libcurl ,您可以使用 CURLOPT_WRITEFUNCTION [ ^ ]将正文数据写入缓冲区或文件(另请参见上述链接末尾的示例)。


您好我为post param写了如下。



 formData = m_curl.BuildForm(_T(  bscreenview),_ T( ),
_T( bscreenssn),_ T ( ),
_T( bscreenbserial),_ T( ),
_T( bscreendomain),_ T( ),
_T( bscreenano),_ T( ),
_T( bscreentype),_ T( ),
_T( hashparams),_ T( pmangHome),
_T( return_url) ,_T( ),
_T( sns),_ T( ),
_T( provider_access_token),_ T(< span class =code-string> ),
_T( provider_refresh_token),_ T( ),
_T( path),_ T( ),
_T( offline),_ T( ),
_T( provider),_ T( ),
NULL);

// formData.Empty();
if (!m_curl.Post(_T( https:// secure.m.pmang.com/?view=login_prc),formData,m_resultHeader,m_resultString))
return 0 ;

我定义为我的curl包装器 class for 获取收到的标题/数据。


size_t CCurl :: header_callback( char * buffer, size_t 大小,
size_t nitems, void * userdata)
{
/ * 收到的标题是nitems * size in'缓冲区'NOT ZERO TERMINATED * /
/ * 'userdata'设置为CURLOPT_HEADERDATA * /
CString * header =(CString *)userdata;
CStringA tempheaderA;
CString tempheader;

tempheaderA = buffer;

tempHeader = tempheaderA;


header.Append(tempheader);

return nitems * size;
}

size_t CCurl :: body_callback( char * buffer , size_t 大小,
size_t nitems, void * userdata)
{
/ * 收到的标题是nitems * size long in '缓冲区'不是零终止* /
/ * 'userdata'设置为CURLOPT_HEADERDATA * /
// CStringA * body =(CStringA *)userdata;

CString * body =(CString *)userdata;
CStringA tempbodyA;
CString tempbody;

tempbodyA = buffer;

tempbody = tempbodyA;


body.Append(tempbody);

return nitems * size;
}





这是代理设置代码:

 CStringA proxyIp = m_proxyAddrA.Mid( 0 ,off); 
curl_easy_setopt(curl,CURLOPT_PROXY,proxyIp.GetString());
int port = atoi(m_proxyAddrA.Mid(off + 1 ,m_proxyAddrA.GetLength() - off - 1 )。GetString());
curl_easy_setopt(curl,CURLOPT_PROXYPORT,port);





没有代理它完全接收标题/格式。

看起来我不知道如何使用libcurl代理。

我应该如何使用libcurl代理?


Hi i am building a simple c++ program using http/https proxy. I tried it to use the MFC CInternetSession class. But it didn't work over many proxy servers. So I tried it to use the libcurl. It seems that working well. But I am tired. When i try to post the form to website. I saw the somethings as follows in smart sniffer.

[2016-01-13 1:49:16:973]
CONNECT m.pmang.com:443 HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
Host: m.pmang.com
Proxy-Connection: Keep-Alive

[2016-01-13 1:49:17:285]
HTTP/1.1 200 Connection established

[2016-01-13 1:49:17:300]
00000000 16 03 01 00 EA 01 00 00 E6 03 01 40 D2 75 C5 FF ........ ...@.u..

My Code also received the "HTTP/1.1 200 Connection established" What is it? What should i do for fix it? Where is Http/Https proxy library working on general proxy servers(50.195.87.31:3128)?

I posted the form content. so I have to receive response html from remote server. for example <html> XXX </html>. But my code receive the "HTTP/1.1 200 Connection established" from proxy server.

I am now using libcurl For example How should i write the code with libcurl?

解决方案

CURLOPT_WRITEFUNCTION is your friend. The function you give libcurl when you set this option is called whenever you get a response from the server after performing the operation.


"HTTP/1.1 200 Connection established" is the initial response line (status line) from the server to your request. Depending on the request it is followed by the header lines and the message body (which you are interested in). You may parse these information to know if the connection was successful and also parse the header line to know about body encoding.

You may have a look at this link: HTTP Made Really Easy[^].

With libcurl, you can use the CURLOPT_WRITEFUNCTION[^] to write the body data to a buffer or file (see also the example at the end of the above link).


Hi I wrote as follows for post param.

            formData = m_curl.BuildForm(_T("bscreenview"), _T(""),
			_T("bscreenssn"), _T(""),
			_T("bscreenbserial"), _T(""),
			_T("bscreendomain"), _T(""),
			_T("bscreenano"), _T(""),
			_T("bscreentype"), _T(""),
			_T("hashparams"), _T("pmangHome"),
			_T("return_url"), _T(""),
			_T("sns"), _T(""),
			_T("provider_access_token"), _T(""),
			_T("provider_refresh_token"), _T(""),
			_T("path"), _T(""),
			_T("offline"), _T(""),
			_T("provider"), _T(""),
			NULL);

		//formData.Empty();
		if (!m_curl.Post(_T("https://secure.m.pmang.com/?view=login_prc"), formData, m_resultHeader, m_resultString))
			return 0;

and I defined to my curl wrapper class for get the received header/data. 


size_t CCurl::header_callback(char *buffer, size_t size,
	size_t nitems, void *userdata)
{
	/* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
	/* 'userdata' is set with CURLOPT_HEADERDATA */
	CString *header = (CString*)userdata;
	CStringA tempheaderA;
	CString tempheader;

	tempheaderA = buffer;

	tempHeader = tempheaderA;


	header.Append(tempheader);

	return nitems * size;
}

size_t CCurl::body_callback(char *buffer, size_t size,
	size_t nitems, void *userdata)
{
	/* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
	/* 'userdata' is set with CURLOPT_HEADERDATA */
	//CStringA *body = (CStringA*)userdata;
	
	CString *body = (CString*)userdata;
	CStringA tempbodyA;
	CString tempbody;

	tempbodyA = buffer;

	tempbody = tempbodyA;


	body.Append(tempbody);

	return nitems * size;
}



It is proxy setting code:

CStringA proxyIp = m_proxyAddrA.Mid(0, off);
			curl_easy_setopt(curl, CURLOPT_PROXY, proxyIp.GetString());
			int port = atoi(m_proxyAddrA.Mid(off + 1, m_proxyAddrA.GetLength() - off - 1).GetString());
			curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);



Without proxy It receive the header/format perfectly.
It look like I don't know about using proxy with libcurl.
How should i use proxy with libcurl?


这篇关于如何在C ++中使用通用的http / https代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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