【正文】
session cookie. But servers might use other means to track sessions, or to detect session hijacking.If the session cookie is not sent in the request, one of two things has gone wrong. Either the cookie was not detected in the previous response, or the cookie was not selected for being sent with the new request.HttpClient automatically parses cookies sent in responses and puts them into a cookie store. HttpClient uses a configurable cookie policy to decide whether a cookie being sent from a server is correct. The default policy plies strictly with RFC 2109, but many servers do not. Play around with the cookie policies until the cookie is accepted and put into the cookie store.If the cookie is accepted from the previous response but still not sent with the new request, make sure that HttpClient uses the same cookie store object. Unless you explicitly manage cookie store objects (not remended for newbies!), this will be the case if you use the same HttpClient object to execute both requests.If the cookie is still not sent with the request, make sure that the URL you are requesting is in the scope for the cookie. Cookies are only sent to the domain and path specified in the cookie scope. A cookie for host will not be sent to host . A cookie for domain . will be sent to both. A cookie for host , without the leading dot, will not be sent to . The latter case can be resolved by using a different cookie spec that adds the leading dot. In the other cases, use a URL that in the cookie scope to establish the session.If the session cookie is sent with the request, but a new session cookie is set in the response anyway, check whether there are cookies other than the session cookie in the request. Some servers are incapable of detecting multiple cookies sent in individual header fields. HttpClient can be advised to put all cookies into a single header field.If that doesn39。t help, you are in trouble. The server may use additional means to track the session, for example the header field named Referer. Set that field to the URL of the previous request. (see this mail)If that doesn39。t help either, you will have to pare the request from your application to a corresponding one generated by a browser. The instructions in step 5 for POST requests apply for GET requests as well. It39。s even simpler with GET, since you don39。t have an entity.Analyze the FormNow it is time to analyze the form defined in the HTML markup of the page. A form in HTML is a set of namevaluepairs called parameters, where some of the values can be entered in the browser. By analyzing the HTML markup, you can learn which parameters you have to define and how to send them to the server.Look for the form tag in the page source. There may be several forms in the page, but they can not be nested. Locate the form you want to submit. Locate the matching /form tag. Everything in between the two may be relevant. Let39。s start with the attributes of the form tag:method=specifies the method used for submitting the form. If it is GET or not specified at all, then you need to create a GET request. The parameters will be added as a query string to the URL. If the method is POST, you need to create a POST request. The parameters will be put in the entity of the request, also referred to as the request body. How to do that is discussed in step 5.action=specifies the URL to which the request has to be sent. Do not try to get this URL from the address bar of your browser! A browser will automatically follow redirects and only displays the final URL, which can be different from the URL in this attribute. It is possible that the URL includes a query string that specifies some parameters. If so, keep that in mind.enctype=specifies the MIME type for the entity of the request generated by the form. The two mon cases are urlencoded (default) and multipartmime. Note that these terms are just informally used here, the exact values that need to be written in an HTML document are specified elsewhere. This attribute is only used for the POST method. If the method is GET, the parameters will always be urlencoded, but not in an entity.acceptcharset=specifies the character set that the browser should allow for user input. It will not be discussed here, but you will have to consider this value if you experience charset related problems.Except for optional query parameters in the action attribute, the parameters of a form are specified by HTML tags between form and /form. The following is a list of tags that can be used to define parameters. Except where stated otherwise, they have a name attribute which specifies the name of the parameter. The value of the parameter usually depends on user input.input type=text name=...input type=password name=...specify singleline input fields. Using the return key in one of these fields will submit the form, so the value really is a single line of input from the user.input type=text readonly name=... value=...input type=hidden name=... value=...specify a parameter that can not be changed by the user. The value of the parameter is given by the value attribute.input type=radio name=... value=...input type=checkbox name=... value=...specify a parameter that can be included or omitted. There usually is more than one tag with the same name. For radio buttons, only one can be selected and the value of the parameter is the value of the selected radio button. For checkboxes, more than one can be selected. There will be one namevaluepair for each selected checkbox, with the same name for all of them.input type=submit name=... value=...button type=submit name=... value=...specify a button to submit the form. The parameter will only be added to the form if that button is used to submit. If another button is used, or the form is submitted by pressing the return key in a text input field, the parameter is not part of the submitted form data.