Home Backend Development PHP Tutorial Cookies,SSL,httpclient的多线程处理,HTTP方法_PHP

Cookies,SSL,httpclient的多线程处理,HTTP方法_PHP

Jun 01, 2016 pm 12:40 PM
httpclient new deal with method server

Cookie


— 作者 sunggsun @ 20:26





8、Cookies

   HttpClient能自动管理cookie,包括允许服务器设置cookie并在需要的时候自动将cookie返回服务器,它也支持手工设置cookie后发送到服务器端。不幸的是,对如何处理cookie,有几个规范互相冲突:Netscape Cookie 草案, RFC2109, RFC2965,而且还有很大数量的软件商的cookie实现不遵循任何规范. 为了处理这种状况,HttpClient提供了策略驱动的cookie管理方式。HttpClient支持的cookie规范有:

Netscape cookie草案,是最早的cookie规范,基于rfc2109。尽管这个规范与rc2109有较大的差别,这样做可以与一些服务器兼容。

rfc2109,是w3c发布的第一个官方cookie规范。理论上讲,所有的服务器在处理cookie(版本1)时,都要遵循此规范,正因如此,HttpClient将其设为默认的规范。遗憾的是,这个规范太严格了,以致很多服务器不正确的实施了该规范或仍在作用Netscape规范。在这种情况下,应使用兼容规范。

兼容性规范,设计用来兼容尽可能多的服务器,即使它们并没有遵循标准规范。当解析cookie出现问题时,应考虑采用兼容性规范。

   RFC2965规范暂时没有被HttpClient支持(在以后的版本为会加上),它定义了cookie版本2,并说明了版本1cookie的不足,RFC2965有意有久取代rfc2109.
  在HttpClient中,有两种方法来指定cookie规范的使用,
HttpClient client = new HttpClient();
client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
这种方法设置的规范只对当前的HttpState有效,参数可取值CookiePolicy.COMPATIBILITY,CookiePolicy.NETSCAPE_DRAFT或CookiePolicy.RFC2109。

System.setProperty("apache.commons.httpclient.cookiespec", "COMPATIBILITY");
此法指的规范,对以后每个新建立的HttpState对象都有效,参数可取值"COMPATIBILITY","NETSCAPE_DRAFT"或"RFC2109"。
  常有不能解析cookie的问题,但更换到兼容规范大都能解决。

  
9、使用HttpClient遇到问题怎么办?


用一个浏览器访问服务器,以确认服务器应答正常

如果在使代理,关掉代理试试

另找一个服务器来试试(如果运行着不同的服务器软件更好)

检查代码是否按教程中讲的思路编写

设置log级别为debug,找出问题出现的原因

打开wiretrace,来追踪客户端与服务器的通信,以确实问题出现在什么地方

用telnet或netcat手工将信息发送到服务器,适合于猜测已经找到了原因而进行试验时

将netcat以监听方式运行,用作服务器以检查httpclient如何处理应答的。

利用最新的httpclient试试,bug可能在最新的版本中修复了

向邮件列表求帮助

向bugzilla报告bug.

  

10、SSL

  借助Java Secure Socket Extension (JSSE),HttpClient全面支持Secure Sockets Layer (SSL)或IETF Transport Layer Security (TLS)协议上的HTTP。JSSE已经jre1.4及以后的版本中,以前的版本则需要手工安装设置,具体过程参见Sun网站或本学习笔记。
  HttpClient中使用SSL非常简单,参考下面两个例子:
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("https://www.verisign.com/");
httpclient.executeMethod(httpget);
System.out.println(httpget.getStatusLine().toString());
,如果通过需要授权的代理,则如下:
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setProxy("myproxyhost", 8080);
httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost",
new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password"));
GetMethod httpget = new GetMethod("https://www.verisign.com/");
httpclient.executeMethod(httpget);
System.out.println(httpget.getStatusLine().toString());

  在HttpClient中定制SSL的步骤如下:

提供了一个实现了org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory接口的socket factory。这个 socket factory负责打一个到服务器的端口,使用标准的或第三方的SSL函数库,并进行象连接握手等初始化操作。通常情况下,这个初始化操作在端口被创建时自动进行的。

实例化一个org.apache.commons.httpclient.protocol.Protocol对象。创建这个实例时,需要一个合法的协议类型(如https),一个定制的socket factory,和一个默认的端中号(如https的443端口).
Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
然后,这个实例可被设置为协议的处理器。
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setHost("www.whatever.com", 443, myhttps);
GetMethod httpget = new GetMethod("/");
httpclient.executeMethod(httpget);


通过调用Protocol.registerProtocol方法,将此定制的实例,注册为某一特定协议的默认的处理器。由此,可以很方便地定制自己的协议类型(如myhttps)。
Protocol.registerProtocol("myhttps",
new Protocol("https", new MySSLSocketFactory(), 9443));
...
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("myhttps://www.whatever.com/");
httpclient.executeMethod(httpget);
如果想用自己定制的处理器取代https默认的处理器,只需要将其注册为"https"即可。
Protocol.registerProtocol("https",
new Protocol("https", new MySSLSocketFactory(), 443));
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("https://www.whatever.com/");
httpclient.executeMethod(httpget);

  已知的限制和问题

持续的SSL连接在Sun的低于1.4JVM上不能工作,这是由于JVM的bug造成。

通过代理访问服务器时,非抢先认证( Non-preemptive authentication)会失败,这是由于HttpClient的设计缺陷造成的,以后的版本中会修改。

  遇到问题的处理
  很多问题,特别是在jvm低于1.4时,是由jsse的安装造成的。
  下面的代码,可作为最终的检测手段。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;

import javax.net.ssl.SSLSocketFactory;

public class Test {

public static final String TARGET_HTTPS_SERVER = "www.verisign.com";
public static final int TARGET_HTTPS_PORT = 443;

public static void main(String[] args) throws Exception {

Socket socket = SSLSocketFactory.getDefault().
createSocket(TARGET_HTTPS_SERVER, TARGET_HTTPS_PORT);
try {
Writer out = new OutputStreamWriter(
socket.getOutputStream(), "ISO-8859-1");
out.write("GET / HTTP/1.1rn");
out.write("Host: " + TARGET_HTTPS_SERVER + ":" +
TARGET_HTTPS_PORT + "rn");
out.write("Agent: SSL-TESTrn");
out.write("rn");
out.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} finally {
socket.close();
}
}
}


  
11、httpclient的多线程处理

  使用多线程的主要目的,是为了实现并行的下载。在httpclient运行的过程中,每个http协议的方法,使用一个HttpConnection实例。由于连接是一种有限的资源,每个连接在某一时刻只能供一个线程和方法使用,所以需要确保在需要时正确地分配连接。HttpClient采用了一种类似jdbc连接池的方法来管理连接,这个管理工作由 MultiThreadedHttpConnectionManager完成。
MultiThreadedHttpConnectionManager connectionManager =
new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);
此是,client可以在多个线程中被用来执行多个方法。每次调用HttpClient.executeMethod() 方法,都会去链接管理器申请一个连接实例,申请成功这个链接实例被签出(checkout),随之在链接使用完后必须归还管理器。管理器支持两个设置: maxConnectionsPerHost 每个主机的最大并行链接数,默认为2
maxTotalConnections 客户端总并行链接最大数,默认为20

  管理器重新利用链接时,采取早归还者先重用的方式(least recently used approach)。
  由于是使用HttpClient的程序而不是HttpClient本身来读取应答包的主体,所以HttpClient无法决定什么时间连接不再使用了,这也就要求在读完应答包的主体后必须手工显式地调用releaseConnection()来释放申请的链接。
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);
...
// 在某个线程中。
GetMethod get = new GetMethod("http://jakarta.apache.org/");
try {
client.executeMethod(get);
// print response to stdout
System.out.println(get.getResponseBodyAsStream());
} finally {
// be sure the connection is released back to the connection
// manager
get.releaseConnection();
}
对每一个HttpClient.executeMethod须有一个method.releaseConnection()与之匹配.

12、HTTP方法


  HttpClient支持的HTTP方法有8种,下面分述之。

  1、Options

  HTTP方法Options用来向服务器发送请求,希望获得针对由请求URL(request url)标志的资源在请求/应答的通信过程可以使用的功能选项。通过这个方法,客户端可以在采取具体行动之前,就可对某一资源决定采取什么动作和/或以及一些必要条件,或者了解服务器提供的功能。这个方法最典型的应用,就是用来获取服务器支持哪些HTTP方法。
  HttpClient中有一个类叫OptionsMethod,来支持这个HTTP方法,利用这个类的getAllowedMethods方法,就可以很简单地实现上述的典型应用。


OptionsMethod options = new OptionsMethod("http://jakarta.apache.org");
// 执行方法并做相应的异常处理
...
Enumeration allowedMethods = options.getAllowedMethods();
options.releaseConnection();

  2、Get

   HTTP方法GET用来取回请求URI(request-URI)标志的任何信息(以实体(entity)的形式),"get"这个单词本意就是”获取“的意思。如果请求URI指向的一个数据处理过程,那这个过程生成的数据,在应答中以实体的形式被返回,而不是将这个过程的代码的返回。
  如果HTTP包中含有If-ModifiedSince, If-Unmodified-Since, If-Match, If-None-Match, 或 If-Range等头字段,则GET也就变成了”条件GET“,即只有满足上述字段描述的条件的实体才被取回,这样可以减少一些非必需的网络传输,或者减少为获取某一资源的多次请求(如第一次检查,第二次下载)。(一般的浏览器,都有一个临时目录,用来缓存一些网页信息,当再次浏览某个页面的时候,只下载那些修改过的内容,以加快浏览速度,就是这个道理。至于检查,则常用比GET更好的方法HEAD来实现。)如果HTTP包中含有Range头字段,那么请求URI指定的实体中,只有决定范围条件的那部分才被取回来。(用过多线程下载工具的朋友,可能比较容易理解这一点)
  这个方法的典型应用,用来从web服务器下载文档。HttpClient定义了一个类叫GetMethod来支持这个方法,用GetMethod类中getResponseBody, getResponseBodyAsStream 或 getResponseBodyAsString函数就可以取到应答包包体中的文档(如HTML页面)信息。这这三个函数中,getResponseBodyAsStream通常是最好的方法,主要是因为它可以避免在处理下载的文档之前缓存所有的下载的数据。

GetMethod get = new GetMethod("http://jakarta.apache.org");
// 执行方法,并处理失败的请求.
...
InputStream in = get.getResponseBodyAsStream();
// 利用输入流来处理信息。
get.releaseConnection();

  对GetMethod的最常见的不正确的使用,是没有将全部的应答主体的数据读出来。还有,必须注意要手工明确地将链接释放。

  3、Head

  HTTP的Head方法,与Get方法完全一致,唯一的差别是服务器不能在应答包中包含主体(message-body),而且一定不能包含主体。使用这个方法,可以使得客户无需将资源下载回就可就以得到一些关于它的基本信息。这个方法常用来检查超链的可访问性以及资源最近有没有被修改。
  HTTP的head方法最典型的应用,是获取资源的基本信息。HttpClient定义了HeadMethod类支持这个方法,HeadMethod类与其它*Method类一样,用 getResponseHeaders()取回头部信息,而没有自己的特殊方法。

HeadMethod head = new HeadMethod("http://jakarta.apache.org");
// 执行方法,并处理失败的请求.
...
// 取回应答包的头字段信息.
Header[] headers = head.getResponseHeaders();

// 只取回最后修改日期字段的信息.
String lastModified = head.getResponseHeader("last-modified").getValue();



  4、Post

  Post在英文有“派驻”的意思,HTTP方法POST就是要求服务器接受请求包中的实体,并将其作为请求URI的下属资源。从本质上说,这意味着服务器要保存这个实体信息,而且通常由服务器端的程序进行处理。Post方法的设计意图,是要以一种统一的方式实现下列功能:
对已有的资源做评注

将信息发布到BBS、新闻组、邮件列表,或类似的文章组中

将一块数据,提交给数据处理进程

通过追加操作,来扩展一个数据库
  这些都操作期待着在服务器端产生一定的“副作用”,如修改了数据库等。
  HttpClient定义PostMethod类以支持该HTTP方法,在httpclient中,使用post方法有两个基本的步骤:为请求包准备数据,然后读取服务器来的应答包的信息。通过调用 setRequestBody()函数,来为请求包提供数据,它可以接收三类参数:输入流、名值对数组或字符串。至于读取应答包需要调用 getResponseBody* 那一系列的方法,与GET方法处理应答包的方法相同。
  常见问题是,没有将全部应答读取(无论它对程序是否有用),或没有释放链接资源。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

See all articles