• 1-描述和简单示例


    可使用的验证码

    a). 知乎的登录界面--验证码 https://www.zhihu.com/captcha.gif?r=1509626035515&type=login
    
    b). 微博注册验证码
    http://diablo.alibaba.com/captcha/click/get.jsonp?sessionid=0152JIZgtMjy7iQLwB8JakWfF-ia0wQJ0XVIoC9KSI9E0tK5acyPEvg7y1foJaZY8nxt3RZ0fXiPaTW4hx2ZxcMW1LHCkTKrSsMSx-K5SQWQIzmgekUSpnBorkOPHqpxetmk0vqI3y4YWx_TGy45OfWg&identity=FFFF00000000016C467E&style=ncc&lang=cn&v=845&callback=jsonp_05557453445615215
    
    c). 博客园登录有图形验证码
    
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.3</version>
            </dependency>
    

    1.0 fluent简化代码

    但是目前不知如何设置代码
    

    1.1 普通post请求

    	public static void main(String[] args) throws IOException {
    
    		// 创建httpclient
    		CloseableHttpClient httpclient = HttpClients.custom().build();
    		// 请求地址和参数
    		HttpPost httpPost = new HttpPost("http://10.253.181.3:32300/syncDataNotify");
    
    		// 设置请求域
    		httpPost.setEntity(new StringEntity(requestContent, Charset.forName("UTF-8")));
    
    		// 打印requestdata
    		System.out.println(requestContent);
    
    		// 设置header信息
    		httpPost.setHeader("Content-Type", "application/soap+xml;
    
    
    		HttpResponse response = httpclient.execute(httpPost);
    
    		// 获取响应实体
    		HttpEntity entity = response.getEntity();
    
    		// 打印响应内容
    				System.out.println("Response content: " + EntityUtils.toString(entity));
    
    		// 关闭连接,释放资源
    		httpclient.close();
        }
    

    1.2 最普通的http请求-代理

    //创建
    CloseableHttpClient httpclient = HttpClients.createDefault();
    //请求地址
    HttpGet httpget = new HttpGet("http://xinsheng.huawei.com/cn/index");
    System.out.println("executing request " + httpget.getURI());
    //执行请求
    HttpResponse response = httpclient.execute(httpget);
    
    //获取响应实体
    HttpEntity entity = response.getEntity();
    

    1.2 设置请求代理(Post请求)-http

    //代理等同在header中设置Proxy-Authorization属性,其中描述用户名和密码(base64编码)
    Proxy-Authorization: Basic ejAwMzU4Nzg0NTQ6MXFheXVpdXo=
    (等同信息z0035878454:1qayuiuz)
    
    	public static void main(String[] args) throws IOException {
    
    		// 证书信息,可添加多个
    		CredentialsProvider credsProvider = new BasicCredentialsProvider();
    		credsProvider.setCredentials(new AuthScope("proxyhk.huawei.com", 8080),
    				new UsernamePasswordCredentials("sdfsdfsd", "sdfsdfsdf"));
    
    		// 创建httpclient
    		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    
    		// 请求地址和参数
    		HttpPost httpPost = new HttpPost("http://www.webservicex.net/geoipservice.asmx");
    
    		// 请求信息中设置使用的代理
    		HttpHost proxy = new HttpHost("proxyhk.huawei.com", 8080);
    		RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    		httpPost.setConfig(config);
    
    		// 请求域信息
    		Document document = DocumentHelper.createDocument();
    
    		// 增加命名空间
    		Namespace sopa12 = Namespace.get("soap12", "http://www.w3.org/2003/05/soap-envelope");
    
    		// 添加带命名空间的节点
    		Element eleSoap12 = document.addElement(new QName("Envelope", sopa12))
    				.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
    				.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    
    		// dom转xml
    		String requestContent = document.asXML();
    
    		//设置请求域
    		httpPost.setEntity(new StringEntity(requestContent, Charset.forName("UTF-8")));
    
    		// 打印requestdata
    		System.out.println(requestContent);
    
    		// 设置header信息
    		httpPost.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
    
    		try {
    			// 请求地址
    			System.out.println("executing request " + httpPost.getURI());
    			// 执行请求
    			HttpResponse response = httpclient.execute(httpPost);
    
    			// 获取响应实体
    			HttpEntity entity = response.getEntity();
    
    			// 打印响应内容长度
    			System.out.println("Response content length: " + entity.getContentLength());
    
    			// 打印响应内容
    			System.out.println("Response content: " + EntityUtils.toString(entity));
    
    		} finally {
    			// 关闭连接,释放资源
    			httpclient.close();
    
    		}
    	}
    

    1.3 发起https请求

    
    
  • 相关阅读:
    php与WebservicesNuSOAP的使用说明(转载)
    获取cpu序列号,硬盘ID,网卡MAC地址(转载)
    system.net.sockets.tcplistener
    用VS2005实现软件多语言版本的迅捷开发(转载)
    洛谷P1776 宝物筛选 题解 多重背包
    2013 ACMICPC亚洲区域赛南京站C题 题解 轮廓线DP
    2017 ACMICPC亚洲区域赛北京站J题 Pangu and Stones 题解 区间DP
    洛谷P1385 密令 题解 动态规划
    洛谷P1028 数的计算 题解 动态规划入门题
    洛谷P1029 最大公约数和最小公倍数问题 题解
  • 原文地址:https://www.cnblogs.com/Desneo/p/7347096.html
Copyright © 2020-2023  润新知