/** * 合并cookie信息放到一个cookie中 此方法会出错,无效。 * * @param client */ private static void mergeCookie(HttpClient client) { Cookie[] cookies = client.getState().getCookies(); if (cookies != null && cookies.length > 0) { String cook = cookies[0].getValue(); for (int i = 1; i < cookies.length; i++) { cook += "; " + cookies[i].getName() + "=" + cookies[i].getValue(); } cookies[0].setValue(cook); HttpState state = new HttpState(); state.addCookie(cookies[0]); client.setState(state); } }
private static String[] httpPost(String url, String xml) throws Exception { HttpClient client = new HttpClient(); HttpState state = client.getState(); Credentials credentials = new UsernamePasswordCredentials("S2VMS", "S2VMS!Q@W#E"); state.setCredentials(null, null, credentials); client.setState(state); PostMethod method = new PostMethod(url); method.setDoAuthentication(true); method.getHostAuthState().setAuthAttempted(true); method.getHostAuthState().setAuthRequested(true); method.getHostAuthState().setPreemptive(); method.addRequestHeader("Content-Type", "text/xml"); method.addRequestHeader("SOAPAction", "http://tempuri.org/GetLinkMedia"); try { method.setRequestBody(xml); } catch (Exception e) { try { ByteArrayRequestEntity entity = new ByteArrayRequestEntity(xml.getBytes()); method.setRequestEntity(entity); } catch (Exception e1) { throw new Exception("Impossible to set the xml in the post"); } } int iRes = client.executeMethod(method); byte[] response = method.getResponseBody(); String textResponse = new String(response); String[] toReturn = {"" + iRes, textResponse}; return toReturn; }
public void proxifyState(HttpState httpState, HostConfiguration hostConfiguration) { Credentials proxyCredentials = _proxyCredentials; String host = hostConfiguration.getHost(); if (isProxyHost(host) && (proxyCredentials != null)) { AuthScope scope = new AuthScope(_PROXY_HOST, _PROXY_PORT, null); httpState.setProxyCredentials(scope, proxyCredentials); } }
protected byte[] URLtoByteArray( String location, Http.Method method, Map<String, String> headers, Cookie[] cookies, Http.Auth auth, Http.Body body, List<Http.FilePart> fileParts, Map<String, String> parts, Http.Response response, boolean followRedirects) throws IOException { byte[] bytes = null; HttpMethod httpMethod = null; HttpState httpState = null; try { _cookies.set(null); if (location == null) { return null; } else if (!location.startsWith(Http.HTTP_WITH_SLASH) && !location.startsWith(Http.HTTPS_WITH_SLASH)) { location = Http.HTTP_WITH_SLASH + location; } HostConfiguration hostConfiguration = getHostConfiguration(location); HttpClient httpClient = getClient(hostConfiguration); if (method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) { if (method.equals(Http.Method.POST)) { httpMethod = new PostMethod(location); } else { httpMethod = new PutMethod(location); } if (body != null) { RequestEntity requestEntity = new StringRequestEntity(body.getContent(), body.getContentType(), body.getCharset()); EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod; entityEnclosingMethod.setRequestEntity(requestEntity); } else if (method.equals(Http.Method.POST)) { PostMethod postMethod = (PostMethod) httpMethod; processPostMethod(postMethod, fileParts, parts); } } else if (method.equals(Http.Method.DELETE)) { httpMethod = new DeleteMethod(location); } else if (method.equals(Http.Method.HEAD)) { httpMethod = new HeadMethod(location); } else { httpMethod = new GetMethod(location); } if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) { httpMethod.addRequestHeader(header.getKey(), header.getValue()); } } if ((method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) && ((body != null) || ((fileParts != null) && !fileParts.isEmpty()) | ((parts != null) && !parts.isEmpty()))) { } else if (!hasRequestHeader(httpMethod, HttpHeaders.CONTENT_TYPE)) { httpMethod.addRequestHeader( HttpHeaders.CONTENT_TYPE, ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED); } if (!hasRequestHeader(httpMethod, HttpHeaders.USER_AGENT)) { httpMethod.addRequestHeader(HttpHeaders.USER_AGENT, _DEFAULT_USER_AGENT); } httpState = new HttpState(); if ((cookies != null) && (cookies.length > 0)) { org.apache.commons.httpclient.Cookie[] commonsCookies = toCommonsCookies(cookies); httpState.addCookies(commonsCookies); HttpMethodParams httpMethodParams = httpMethod.getParams(); httpMethodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); } if (auth != null) { httpMethod.setDoAuthentication(true); httpState.setCredentials( new AuthScope(auth.getHost(), auth.getPort(), auth.getRealm()), new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword())); } proxifyState(httpState, hostConfiguration); httpClient.executeMethod(hostConfiguration, httpMethod, httpState); Header locationHeader = httpMethod.getResponseHeader("location"); if ((locationHeader != null) && !locationHeader.equals(location)) { String redirect = locationHeader.getValue(); if (followRedirects) { return URLtoByteArray( redirect, Http.Method.GET, headers, cookies, auth, body, fileParts, parts, response, followRedirects); } else { response.setRedirect(redirect); } } InputStream inputStream = httpMethod.getResponseBodyAsStream(); if (inputStream != null) { Header contentLength = httpMethod.getResponseHeader(HttpHeaders.CONTENT_LENGTH); if (contentLength != null) { response.setContentLength(GetterUtil.getInteger(contentLength.getValue())); } Header contentType = httpMethod.getResponseHeader(HttpHeaders.CONTENT_TYPE); if (contentType != null) { response.setContentType(contentType.getValue()); } bytes = FileUtil.getBytes(inputStream); } for (Header header : httpMethod.getResponseHeaders()) { response.addHeader(header.getName(), header.getValue()); } return bytes; } finally { try { if (httpState != null) { _cookies.set(toServletCookies(httpState.getCookies())); } } catch (Exception e) { _log.error(e, e); } try { if (httpMethod != null) { httpMethod.releaseConnection(); } } catch (Exception e) { _log.error(e, e); } } }