Data crawling using HTTP proxy ip code example
HTTP Proxy Demo code
1 、 Python
#!-*-encoding:utf-8-*-the target page to be accessed by import requests# targetUrl = "http://ip.do.cn/ip"# proxy server proxyHost =" ip.do.cn "proxyPort =" 39010 "# proxy tunnel authentication information proxyUser =" username "proxyPass =" password "proxyMeta =" http://%(user)s:%(pass)s@%(host)s:%(port)s"% {"host": proxyHost "port": proxyPort, user: proxyUser, "pass": proxyPass,} proxies= {"http": proxyMeta, "https": proxyMeta,} resp = requests.get (targetUrl, proxies=proxies) print resp.status_codeprint resp.text
2 、 C Sharp
HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://ip.do.cn/ip");WebProxy myProxy = new WebProxy (); Uri newUri = newUri (" http://ip.do.cn:39010");myProxy.Address = newUri;myProxy.Credentials = new NetworkCredential ("username", "password"); request.Proxy = myProxy
3 、 PHP
/ / Target page $targetUrl = "http://ip.do.cn/ip";//$targetUrl =" http://ip.do.cn/switch-ip";//$targetUrl = "http://ip.do.cn/current-ip";// proxy server define (" PROXY_SERVER "," ip.do.cn:39010 "); / / Tunnel identity information define (" PROXY_USER "," username "); define (" PROXY_PASS "," password ") $proxyAuth = base64_encode (PROXY_USER. ":" PROXY_PASS); $headers = implode ("\ r\ n", ["Proxy-Authorization: Basic {$proxyAuth}", "Proxy-Switch-Ip: yes",]); $options = ["http" = > ["proxy" = > $proxyServer, "header" = > $headers, "method" = > "GET",],]; $context = stream_context_create ($options); $result = file_get_contents ($url, false, $context); var_dump ($result)
4 、 JAVA
Import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.Authenticator;import java.net.HttpURLConnection;import java.net.InetSocketAddress;import java.net.PasswordAuthentication;import java.net.Proxy;import java.net.URL;class ProxyAuthenticator extends Authenticator {private String user, password; public ProxyAuthenticator (String user, String password) {this.user = user; this.password = password } protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (user, password.toCharArray ()) }} / * Note: the following code only implements the HTTP request link, and each request is stateless, except that this request changes the IP, if the IP address of the next request will change * if it is multithreaded access, as long as the following code is embedded in your own business logic, then the new IP will be used for access each time, if you are worried that the IP is duplicated * you can maintain the usage of IP and verify it. * / public class ProxyDemo {public static void main (String args []) throws Exception {/ / destination page to visit String targetUrl = "http://ip.do.cn/ip"; / / String targetUrl =" http://ip.do.cn/switch-ip"; / / String targetUrl = "http://ip.do.cn/current-ip"; / / proxy server String proxyServer = "ip.do.cn"; int proxyPort = 39010; / / proxy tunnel authentication information String proxyUser = "username"; String proxyPass = "password"; try {URL url = new URL (targetUrl); Authenticator.setDefault (new ProxyAuthenticator (proxyUser, proxyPass)) / / create proxy server address object InetSocketAddress addr = new InetSocketAddress (proxyServer, proxyPort); / / create HTTP type proxy object Proxy proxy = new Proxy (Proxy.Type.HTTP, addr); / / set to access the target page HttpURLConnection connection = (HttpURLConnection) url.openConnection (proxy) through proxy / / set IP switch header connection.setRequestProperty ("Proxy-Switch-Ip", "yes"); / / parse the returned data byte [] response = readStream (connection.getInputStream ()); System.out.println (new String (response));} catch (Exception e) {System.out.println (e.getLocalizedMessage ()) }} / * convert the input stream into a string * * @ param inStream * @ return * @ throws Exception * / public static byte [] readStream (InputStream inStream) throws Exception {ByteArrayOutputStream outSteam = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len =-1 While ((len = inStream.read (buffer))! =-1) {outSteam.write (buffer, 0, len);} outSteam.close (); inStream.close (); return outSteam.toByteArray ();}}
5 、 golang
Package mainimport ("net/url"net/http"bytes"fmt"io/ioutil") const ProxyServer = "ip.do.cn:39010" type ProxyAuth struct {License string SecretKey string} func (p ProxyAuth) ProxyClient () http.Client {proxyURL _: = url.Parse ("http://" + p.License +": "+ p.SecretKey +" @ "+ ProxyServer) return http.Client {Transport: & http.Transport {Proxy:http.ProxyURL (proxyURL)}} func main () {targetURI: =" http://ip.od.cn/ip" / / targetURI: = "http://ip.od.cn/switch-ip" / / targetURI: =" http://ip. Hahaod.cn/current-ip "/ / initialize proxy http client client: = ProxyAuth {License:" username " SecretKey: "password"}. ProxyClient () request, _: = http.NewRequest ("GET", targetURI, bytes.NewBuffer ([] byte (``) / / switch IP (only HTTP is supported) request.Header.Set ("Proxy-Switch-Ip", "yes") response, err: = client.Do (request) if err! = nil {panic ("failed to connect:" + err.Error ())} else {bodyByte Err: = ioutil.ReadAll (response.Body) if err! = nil {fmt.Println ("error reading Body", err) return} response.Body.Close () body: = string (bodyByte) fmt.Println ("Response Status:", response.Status) fmt.Println ("Response Header:", response.Header) fmt.Println ("Response Body:\ n" Body)}} for more agent settings, please copy the connection and open: https://v.duoip.cn/customer/signup/?sale=xujinyang1991