Summary of the invention
Therefore, for above-mentioned problem, the present invention proposes the method for the upper guiding of the HTTP flow for APP of a kind of iOS, and solve APP and use the primary HTTP API of the iOS the superiors, but the problem of wanting not to be subject to overall situation agency's impact or wanting own authorized agency.
Concrete, the technical solution adopted in the present invention is that the method for the upper guiding of the HTTP flow for APP of a kind of iOS, comprises the following steps:
Step 1:iOS system Real-Time Monitoring HTTP flow, if there is HTTP flow, record produces the application software (for example needing the APP application software of acceleration or direct traffic) of HTTP flow, the application software of this generation HTTP flow is designated as to HTTP_APP, tackles primary HTTP API simultaneously and obtain the function (function SCDynamicStoreCopyProxies) that the system overall situation is acted on behalf of;
Step 2: structure proxy function, this proxy function realizes primary HTTP API obtains all functions of system overall situation agency's function (function SCDynamicStoreCopyProxies), after this proxy function is set up, by primary HTTP API Calls, realizes agency; This proxy function is returned to the structure that comprises proxy information, and this proxy information at least comprises proxy, proxy port;
Step 3: search the mirror image head of application software HTTP_APP in the middle of internal memory, find the function (function name is SCDynamicStoreCopyProxies) of the proxy information that obtains current network, by the function address of structure in the address replacement step 2 of this function;
When the primary HTTP API of step 4:iOS system initiates HTTP request, the proxy function that invocation step 2 is constructed, this function will return to self-defining proxy information, and the validity of proxy information is detected; In the time that the address in proxy information is effective, will connect the address of this appointment, and HTTP request is configured to HTTP Proxy request mails to the proxy server of appointment, thereby realize authorized agency, also just realize the guiding of HTTP flow.
In addition, also said process realization can be packaged into a SDK calls for APP application software, APP application software startup stage call this SDK and realize this API of SCDynamicStoreCopyProxies interception, and proxy server address is set, next all HTTP requests of APP will be directed into set proxy server.
Wherein, the HTTP request that the primary HTTP API of iOS system initiates, first by preliminary treatment, and then send, this preliminary treatment comprises if process: whether first this HTTP request detects this HTTP asks the domain name that will access identical with the domain name of HTTP_APP, if both are not identical, ask the domain name that will access to change the domain name of HTTP_APP into HTTP; Whether the authorized agency's row (a line after stem <headers>) that then detects HTTP request is empty, if be not empty, is changed into blank line, and then this HTTP request is sent.By above-mentioned setting, can safeguard the validity of HTTP request, when avoiding follow-up authorized agency, occur that agency makes mistakes, and cause the method not use, and then cause cannot using of HTTP_APP.
Further, in described step 1, tackle primary HTTP API and obtain the function that the system overall situation is acted on behalf of, it comprises following process:
Step 11: the address that obtains HTTP_APP process;
Step 12: search the mirror image head of HTTP_APP in internal memory, obtain the address of system overall situation agency's function (function SCDynamicStoreCopyProxies), this address is replaced with to the function address that obtains proxy information of oneself constructing, and then realize the interception of the overall proxy function to primary HTTP API.
In addition, regenerate and manage and must arrange by system in iOS incision, relatively trouble.By scheme, also can switch HTTP Proxy in any time of APP operation phase.Therefore, in described step 4, the validity in proxy information is detected, when detecting that agency is while going wrong, can switch immediately agent address or authorized agency address not, thereby ensure the normal operation of said method.Step is as follows:
Step 41: the validity in proxy information is detected, if proxy information is invalid, detects HTTP request and whether make a mistake;
Step 42: if HTTP request makes a mistake, whether first detect http proxy server goes wrong, if proxy server goes wrong, change proxy server or ban use of authorized agency to make HTTP ask direct Hui Yuan by agent address is set immediately; If proxy server does not go wrong, automatically resend HTTP request.
The present invention adopts such scheme, and solve APP and use the primary HTTP API of the iOS the superiors, but the problem of wanting not to be subject to overall situation agency's impact or wanting own authorized agency; For APP developer provides extremely easy mode, need not change HTTP request code and can realize agency simultaneously; In the time that system overall situation agency goes wrong, can act on behalf of direct Hui Yuan by bypass system, thereby APP can normally be moved.
Embodiment
Now in conjunction with embodiment, the present invention is further described.
The invention provides the method for flow guiding, build an iOS SDK and call for iOS APP, make APP itself can oneself determine want to use overall situation agency or use the agency who oneself disposes.SDK possesses following two large functions: (1) is tackled primary HTTP API and obtained the function that the system overall situation is acted on behalf of.(2) structure authorized agency's data return to HTTP API.
Concrete, the technical solution adopted in the present invention is that the method for the upper guiding of the HTTP flow for APP of a kind of iOS, comprises the following steps:
Step 1:iOS system Real-Time Monitoring HTTP flow, if there is HTTP flow, record produces the application software (for example needing the APP application software of acceleration or direct traffic) of HTTP flow, the application software of this generation HTTP flow is designated as to HTTP_APP, tackles primary HTTP API simultaneously and obtain the function (function SCDynamicStoreCopyProxies) that the system overall situation is acted on behalf of;
Step 2: structure proxy function, this proxy function realizes primary HTTP API obtains all functions of system overall situation agency's function (function SCDynamicStoreCopyProxies), after this proxy function is set up, by primary HTTP API Calls, realizes agency; This proxy function is returned to the structure that comprises proxy information, and this proxy information at least comprises proxy, proxy port etc.;
When structure proxy function, first realize a function SCDynamicStoreCopyProxies(function performance with primary HTTP API: the configuration information of acting on behalf of of returning to current network) identical function my_SCDynamicStoreCopyProxies, this function returns to the structure that comprises proxy information, and this dictionary is acted on behalf of for realizing by HTTP API messenger; Wherein, the dictionary that has comprised proxy information is the dictionary of a json form, and form is as follows:
{
" HTTPEnable ": 1(int type)
" HTTPProxy ": " http_proxy_host " (String type)
" HTTPPort ": http_proxy_port(int type)
" HTTPSEnable ": 1(int type)
" HTTPSProxy ": " https_proxy_host " (String type)
" HTTPSPort ": https_proxy_port (int type)
};
Step 3: the mirror image head of searching application software HTTP_APP, find the function (function name is " SCDynamicStoreCopyProxies ") of the proxy information that obtains current network, the address of this function is replaced with to the function address of structure in step 2, when thereby HTTP_APP calls SCDynamicStoreCopyProxies function, automatically point to my_SCDynamicStoreCopyProxies, thereby can return to the agent address of appointment;
First step 4:iOS APP carries out preliminary treatment while calling the upper strata HTTP API initiation HTTP requests such as NSURL, and then send, API inside will be called SCDynamicStoreCopyProxies and be obtained system overall situation agency, but through interception (function replacement) afterwards, this function will return to self-defining proxy server address, and the validity in proxy information is detected.In the time that agent address is effective, will connect upper agent address of specifying, and HTTP is asked to be configured to HTTP Proxy request mail to the proxy server of appointment, thereby realize agent functionality, also just realize HTTP flow and guided.
Step 5: said process realization is packaged into a SDK and calls for APP, APP startup stage call SDK and realize this API of SCDynamicStoreCopyProxies interception, and proxy server address is set, next all HTTP requests of APP will be directed into set proxy server.
HTTP request is generally made up of two parts: request and response, and the form of HTTP request is as follows:
<request-line>
<headers>
<blank?line>
[<request-body>]
In HTTP request, define multiple request type, most importantly GET request and POST request.In HTTP request, the first row must be a request row (request line), is used for the resource (root of domain name or domain name) that request type is described, will accesses and the HTTP version using.And then be a stem (header) trifle, be used for illustrating the additional information that server (destination of request) will use.After stem, be a null, this row can be to specify the User-Agent of agency, and server end and client script can both be accessed this row, and it is the important foundation that browser type detects logic.After this, can add other data [being referred to as main body (body)] arbitrarily again.For example on Web browser, input a URL, browser just will send a GET request to server based on this URL, to tell server to obtain and to return to what resource,
In above-mentioned steps 4, the HTTP request that the primary HTTP API of iOS system initiates, first by preliminary treatment, and then send, this preliminary treatment comprises if process: whether first this HTTP request detects this HTTP asks the domain name that will access identical with the domain name of HTTP_APP, if both are not identical, ask the domain name that will access to change the domain name of HTTP_APP into HTTP; Whether the authorized agency's row (a line after stem <headers>) that then detects HTTP request is empty, if be not empty, is changed into blank line, and then this HTTP request is sent.By above-mentioned setting, can safeguard the validity of HTTP request, when avoiding follow-up authorized agency, occur that agency makes mistakes, and cause the method not use, and then cause cannot using of HTTP_APP.
In described step 4, the validity in proxy information is detected, when detecting that agency is while going wrong, can switch immediately agent address or authorized agency address not, thereby ensure the normal operation of said method.Step is as follows:
Step 41: the validity in proxy information is detected, if proxy information is invalid, detects HTTP request and whether make a mistake;
Step 42: if HTTP request makes a mistake, whether first detect http proxy server goes wrong, if proxy server goes wrong, agent address is set changes proxy server or ban use of authorized agency to make HTTP ask direct Hui Yuan by calling SDK immediately; If proxy server does not go wrong, automatically resend HTTP request.
The present invention is by re-constructing an identical function of function SCDynamicStoreCopyProxies with the primary HTTP API of iOS system, and proxy information is wherein replaced with to user-defined information, can realize two kinds of effects:
(1) break away from overall situation agency: " HTTPEnable " and " HTTPSEnable " is set to 0 or " HTTPProxy " and " HTTPSProxy " is set to nil, " HTTPPort " and " HTTPSPort " is set to 0;
(2) authorized agency: " HTTPEnable " and " HTTPSEnable " is set to 1, respectively " HTTPProxy ", " HTTPPort ", " HTTPSProxy " and " HTTPSPort " is set to the proxy information of wanting appointment.
Although specifically show and introduced the present invention in conjunction with preferred embodiment; but those skilled in the art should be understood that; not departing from the spirit and scope of the present invention that appended claims limits; can make a variety of changes the present invention in the form and details, be protection scope of the present invention.