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请求


posted @ 2017-08-11 17:31  Desneo  阅读(369)  评论(0编辑  收藏  举报