List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBody
public abstract byte[] getResponseBody() throws IOException;
From source file:com.ideabase.repository.webservice.client.impl.HttpWebServiceControllerImpl.java
/** * {@inheritDoc}// w w w.j a v a2s . c o m */ public WebServiceResponse sendServiceRequest(final WebServiceRequest pRequest) { if (LOG.isDebugEnabled()) { LOG.debug("Sending request - " + pRequest); } // build query string final NameValuePair[] parameters = buildParameters(pRequest); // Send request to server final HttpMethod httpMethod = prepareMethod(pRequest.getRequestMethod(), pRequest.getServiceUri(), parameters); // set cookie policy httpMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2109); // set cookies if (mCookies != null) { httpMethod.setRequestHeader(HEADER_COOKIE, mCookies); } if (pRequest.getCookies() != null) { httpMethod.setRequestHeader(HEADER_COOKIE, pRequest.getCookies()); mCookies = pRequest.getCookies(); } // execute method final HttpClient httpClient = new HttpClient(); final WebServiceResponse response; try { final int status = httpClient.executeMethod(httpMethod); // build web sercie response response = new WebServiceResponse(); response.setResponseStatus(status); response.setResponseContent(new String(httpMethod.getResponseBody())); response.setContentType(httpMethod.getResponseHeader(HEADER_CONTENT_TYPE).getValue()); response.setServiceUri(httpMethod.getURI().toString()); // set cookies final Header cookieHeader = httpMethod.getResponseHeader(HEADER_SET_COOKIE); if (cookieHeader != null) { mCookies = cookieHeader.getValue(); } // set cookies to the returning response object. response.setCookies(mCookies); if (LOG.isDebugEnabled()) { LOG.debug("Cookies - " + mCookies); LOG.debug("Response - " + response); } } catch (Exception e) { throw ServiceException.aNew(pRequest, "Failed to send web service request.", e); } finally { httpMethod.releaseConnection(); } return response; }
From source file:it.infn.ct.aleph_portlet.java
public void getRecordsOAR(String search, int jrec, int num_rec) { String responseXML = null;//from w ww . ja v a 2 s . c om HttpClient client = new HttpClient(); HttpMethod method = callAPIOAR(search, jrec, num_rec); try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { method.getResponseBody(); responseXML = convertStreamToString(method.getResponseBodyAsStream()); FileWriter fw = new FileWriter( appServerPath + "datatable/marcXML_OAR_" + jrec + "_" + num_rec + ".xml"); System.out.println(); fw.append(responseXML); fw.close(); } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); } }
From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java
/** * Http/*from w w w . ja va 2s . c o m*/ * * @param request ? * @param strParaFileName ??? * @param strFilePath * @return * @throws HttpException, IOException */ public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath) throws HttpException, IOException { HttpClient httpclient = new HttpClient(connectionManager); // int connectionTimeout = defaultConnectionTimeout; if (request.getConnectionTimeout() > 0) { connectionTimeout = request.getConnectionTimeout(); } httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); // int soTimeout = defaultSoTimeout; if (request.getTimeout() > 0) { soTimeout = request.getTimeout(); } httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); // ConnectionManagerconnection httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); String charset = request.getCharset(); charset = charset == null ? DEFAULT_CHARSET : charset; HttpMethod method = null; //get?? if (request.getMethod().equals(HttpRequest.METHOD_GET)) { method = new GetMethod(request.getUrl()); method.getParams().setCredentialCharset(charset); // parseNotifyConfig??GETrequestQueryString method.setQueryString(request.getQueryString()); } else if (strParaFileName.equals("") && strFilePath.equals("")) { //post?? method = new PostMethod(request.getUrl()); ((PostMethod) method).addParameters(request.getParameters()); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=" + charset); } else { //post? method = new PostMethod(request.getUrl()); List<Part> parts = new ArrayList<Part>(); for (int i = 0; i < request.getParameters().length; i++) { parts.add(new StringPart(request.getParameters()[i].getName(), request.getParameters()[i].getValue(), charset)); } //?strParaFileName??? parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath)))); // ((PostMethod) method).setRequestEntity( new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams())); } // Http HeaderUser-Agent method.addRequestHeader("User-Agent", "Mozilla/4.0"); HttpResponse response = new HttpResponse(); try { httpclient.executeMethod(method); if (request.getResultType().equals(HttpResultType.STRING)) { response.setStringResult(method.getResponseBodyAsString()); } else if (request.getResultType().equals(HttpResultType.BYTES)) { response.setByteResult(method.getResponseBody()); } response.setResponseHeaders(method.getResponseHeaders()); } catch (UnknownHostException ex) { return null; } catch (IOException ex) { return null; } catch (Exception ex) { return null; } finally { method.releaseConnection(); } return response; }
From source file:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java
/** * @see it.greenvulcano.gvesb.virtual.CallOperation#perform(it.greenvulcano.gvesb.buffer.GVBuffer) *//*from w w w.j a v a 2s . c o m*/ @Override public GVBuffer perform(GVBuffer gvBuffer) throws ConnectionException, CallException, InvalidDataException { logger.debug("BEGIN perform(GVBuffer gvBuffer)"); HttpMethod method = null; try { String currMethodURI = null; Map<String, Object> params = GVBufferPropertiesHelper.getPropertiesMapSO(gvBuffer, true); String currHost = PropertiesHandler.expand(host, params, gvBuffer); String currPort = PropertiesHandler.expand(port, params, gvBuffer); logger.debug("Server Host: " + currHost + " - Port: " + currPort); httpClient.getHostConfiguration().setHost(currHost, Integer.parseInt(currPort), protocol); auth.setAuthentication(httpClient, host, Integer.parseInt(currPort), gvBuffer, params); proxy.setProxy(httpClient, gvBuffer, params); currMethodURI = PropertiesHandler.expand(contextPath + methodURI, params, gvBuffer); logger.debug("MethodURI[escaped:" + uriEscaped + "]=[" + currMethodURI + "]"); switch (methodName) { case OPTIONS: method = new OptionsMethod(); break; case GET: method = new GetMethod(); break; case HEAD: method = new HeadMethod(); break; case POST: method = new PostMethod(); break; case PUT: method = new PutMethod(); break; case DELETE: method = new DeleteMethod(); break; default: throw new CallException("GV_CALL_SERVICE_ERROR", new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() }, { "id", gvBuffer.getId().toString() }, { "message", "Unknown method = " + methodName } }); } method.setURI(new URI(currMethodURI, uriEscaped)); if ((refDP != null) && (refDP.length() > 0)) { logger.debug("Calling configured Data Provider: " + refDP); DataProviderManager dataProviderManager = DataProviderManager.instance(); IDataProvider dataProvider = dataProviderManager.getDataProvider(refDP); try { dataProvider.setContext(method); dataProvider.setObject(gvBuffer); method = (HttpMethod) dataProvider.getResult(); } finally { dataProviderManager.releaseDataProvider(refDP, dataProvider); } } int status = httpClient.executeMethod(method); gvBuffer.setProperty(RESPONSE_STATUS, String.valueOf(status)); String statusTxt = method.getStatusText(); gvBuffer.setProperty(RESPONSE_MESSAGE, (statusTxt != null ? statusTxt : "NULL")); Header[] responseHeaders = method.getResponseHeaders(); for (Header header : responseHeaders) { String headerName = RESPONSE_HEADER_PREFIX + header.getName(); String value = header.getValue(); if (value == null) { value = ""; } gvBuffer.setProperty(headerName, value); } String cType = "text/html"; Header cTypeHeader = method.getResponseHeader("Content-Type"); if (cTypeHeader != null) { String cTypeValue = cTypeHeader.getValue(); if (cTypeValue != null) { cType = cTypeValue; } } logger.debug("Response content-type: " + cType); ContentType contentType = new ContentType(cType); byte[] responseBody = method.getResponseBody(); Object object = responseBody; if (contentType.getPrimaryType().equals("multipart")) { object = handleMultipart(responseBody, cType); } gvBuffer.setObject(object); } catch (CallException exc) { throw exc; } catch (Exception exc) { logger.error("ERROR perform(GVBuffer gvBuffer)", exc); throw new CallException("GV_CALL_SERVICE_ERROR", new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() }, { "id", gvBuffer.getId().toString() }, { "message", exc.getMessage() } }, exc); } finally { try { if (method != null) { method.releaseConnection(); } } catch (Exception exc) { logger.warn("Error while releasing connection", exc); } logger.debug("END perform(GVBuffer gvBuffer)"); } return gvBuffer; }
From source file:com.qlkh.client.server.proxy.ProxyServlet.java
/** * Executes the {@link org.apache.commons.httpclient.HttpMethod} passed in and sends the proxy response * back to the client via the given {@link javax.servlet.http.HttpServletResponse} * * @param httpMethodProxyRequest An object representing the proxy request to be made * @param httpServletResponse An object by which we can send the proxied * response back to the client * @throws java.io.IOException Can be thrown by the {@link org.apache.commons.httpclient.HttpClient}.executeMethod * @throws javax.servlet.ServletException Can be thrown to indicate that another error has occurred *///from w ww.j av a2 s . c o m private void executeProxyRequest(HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { if (httpServletRequest.isSecure()) { Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); } // Create a default HttpClient HttpClient httpClient = new HttpClient(); httpMethodProxyRequest.setFollowRedirects(false); // Execute the request int intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest); InputStream response = httpMethodProxyRequest.getResponseBodyAsStream(); // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect // Hooray for open source software if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { String stringStatusCode = Integer.toString(intProxyResponseCode); String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); if (stringLocation == null) { throw new ServletException("Received status code: " + stringStatusCode + " but no " + STRING_LOCATION_HEADER + " header was found in the response"); } // Modify the redirect to go to this proxy servlet rather that the proxied host String stringMyHostName = httpServletRequest.getServerName(); if (httpServletRequest.getServerPort() != 80) { stringMyHostName += ":" + httpServletRequest.getServerPort(); } stringMyHostName += httpServletRequest.getContextPath(); if (followRedirects) { if (stringLocation.contains("jsessionid")) { Cookie cookie = new Cookie("JSESSIONID", stringLocation.substring(stringLocation.indexOf("jsessionid=") + 11)); cookie.setPath("/"); httpServletResponse.addCookie(cookie); //debug("redirecting: set jessionid (" + cookie.getValue() + ") cookie from URL"); } else if (httpMethodProxyRequest.getResponseHeader("Set-Cookie") != null) { Header header = httpMethodProxyRequest.getResponseHeader("Set-Cookie"); String[] cookieDetails = header.getValue().split(";"); String[] nameValue = cookieDetails[0].split("="); Cookie cookie = new Cookie(nameValue[0], nameValue[1]); cookie.setPath("/"); //debug("redirecting: setting cookie: " + cookie.getName() + ":" + cookie.getValue() + " on " + cookie.getPath()); httpServletResponse.addCookie(cookie); } httpServletResponse.sendRedirect( stringLocation.replace(getProxyHostAndPort() + this.getProxyPath(), stringMyHostName)); return; } } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) { // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0); httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } // Pass the response code back to the client httpServletResponse.setStatus(intProxyResponseCode); // Pass response headers back to the client Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders(); for (Header header : headerArrayResponse) { if (header.getName().equals("Transfer-Encoding") && header.getValue().equals("chunked") || header.getName().equals("Content-Encoding") && header.getValue().equals("gzip") || // don't copy gzip header header.getName().equals("WWW-Authenticate")) { // don't copy WWW-Authenticate header so browser doesn't prompt on failed basic auth // proxy servlet does not support chunked encoding } else { httpServletResponse.setHeader(header.getName(), header.getValue()); } } List<Header> responseHeaders = Arrays.asList(headerArrayResponse); if (isBodyParameterGzipped(responseHeaders)) { debug("GZipped: true"); int length = 0; if (!followRedirects && intProxyResponseCode == HttpServletResponse.SC_MOVED_TEMPORARILY) { String gz = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); httpServletResponse.setStatus(HttpServletResponse.SC_OK); intProxyResponseCode = HttpServletResponse.SC_OK; httpServletResponse.setHeader(STRING_LOCATION_HEADER, gz); } else { final byte[] bytes = ungzip(httpMethodProxyRequest.getResponseBody()); length = bytes.length; response = new ByteArrayInputStream(bytes); } httpServletResponse.setContentLength(length); } // Send the content to the client debug("Received status code: " + intProxyResponseCode, "Response: " + response); //httpServletResponse.getWriter().write(response); copy(response, httpServletResponse.getOutputStream()); }
From source file:com.sourcesense.confluence.servlets.CMISProxyServlet.java
/** * Executes the {@link HttpMethod} passed in and sends the proxy response * back to the client via the given {@link HttpServletResponse} * * @param httpMethodProxyRequest An object representing the proxy request to be made * @param httpServletResponse An object by which we can send the proxied * response back to the client * @param httpServletRequest Request object pertaining to the proxied HTTP request * @throws IOException Can be thrown by the {@link HttpClient}.executeMethod * @throws ServletException Can be thrown to indicate that another error has occurred */// w ww . ja va 2 s.c o m private void executeProxyRequest(HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { // Create a default HttpClient HttpClient httpClient = new HttpClient(); getCredential(httpServletRequest.getParameter("servername")); if (credentials != null) { httpClient.getParams().setAuthenticationPreemptive(true); httpClient.getState().setCredentials(AuthScope.ANY, credentials); } httpMethodProxyRequest.setFollowRedirects(true); // Execute the request int intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest); String response = httpMethodProxyRequest.getResponseBodyAsString(); // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect // Hooray for open source software if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { String stringStatusCode = Integer.toString(intProxyResponseCode); String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); if (stringLocation == null) { throw new ServletException("Received status code: " + stringStatusCode + " but no " + STRING_LOCATION_HEADER + " header was found in the response"); } // Modify the redirect to go to this proxy servlet rather that the proxied host String stringMyHostName = httpServletRequest.getServerName(); if (httpServletRequest.getServerPort() != 80) { stringMyHostName += ":" + httpServletRequest.getServerPort(); } stringMyHostName += httpServletRequest.getContextPath(); if (followRedirects) { if (stringLocation.contains("jsessionid")) { Cookie cookie = new Cookie("JSESSIONID", stringLocation.substring(stringLocation.indexOf("jsessionid=") + 11)); cookie.setPath("/"); httpServletResponse.addCookie(cookie); //debug("redirecting: set jessionid (" + cookie.getValue() + ") cookie from URL"); } else if (httpMethodProxyRequest.getResponseHeader("Set-Cookie") != null) { Header header = httpMethodProxyRequest.getResponseHeader("Set-Cookie"); String[] cookieDetails = header.getValue().split(";"); String[] nameValue = cookieDetails[0].split("="); Cookie cookie = new Cookie(nameValue[0], nameValue[1]); cookie.setPath("/"); //debug("redirecting: setting cookie: " + cookie.getName() + ":" + cookie.getValue() + " on " + cookie.getPath()); httpServletResponse.addCookie(cookie); } httpServletResponse.sendRedirect(stringLocation .replace(getProxyHostAndPort(httpServletRequest) + this.getProxyPath(), stringMyHostName)); return; } } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) { // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0); httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } // Pass the response code back to the client httpServletResponse.setStatus(intProxyResponseCode); // Pass response headers back to the client Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders(); for (Header header : headerArrayResponse) { if (header.getName().equals("Transfer-Encoding") && header.getValue().equals("chunked") || header.getName().equals("Content-Encoding") && header.getValue().equals("gzip") || // don't copy gzip header header.getName().equals("WWW-Authenticate")) { // don't copy WWW-Authenticate header so browser doesn't prompt on failed basic auth // proxy servlet does not support chunked encoding } else { httpServletResponse.setHeader(header.getName(), header.getValue()); } } List<Header> responseHeaders = Arrays.asList(headerArrayResponse); if (isBodyParameterGzipped(responseHeaders)) { debug("GZipped: true"); if (!followRedirects && intProxyResponseCode == HttpServletResponse.SC_MOVED_TEMPORARILY) { response = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); httpServletResponse.setStatus(HttpServletResponse.SC_OK); intProxyResponseCode = HttpServletResponse.SC_OK; httpServletResponse.setHeader(STRING_LOCATION_HEADER, response); } else { response = new String(ungzip(httpMethodProxyRequest.getResponseBody())); } httpServletResponse.setContentLength(response.length()); } // Send the content to the client if (intProxyResponseCode == 200) httpServletResponse.getWriter().write(response); else httpServletResponse.getWriter().write(intProxyResponseCode); }
From source file:nl.nn.adapterframework.http.HttpSender.java
public String getResponseBodyAsBase64(HttpMethod httpmethod) throws IOException { byte[] bytes = httpmethod.getResponseBody(); if (bytes == null) { return null; }/*from www . j a v a2 s .c o m*/ if (log.isDebugEnabled()) log.debug(getLogPrefix() + "base64 encodes response body"); return Base64.encode(bytes); }
From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java
public HttpResponse submitRequest(HttpMethod req, final RequestContext rq) throws HttpException, IOException { try {//from w w w.j av a 2 s . com final long start = System.currentTimeMillis(); final HttpRequestCallback<HttpResponse> callback = new HttpRequestCallback<HttpResponse>() { @Override public HttpResponse onCallSuccess(HttpMethod method) throws Exception { long end = System.currentTimeMillis(); Map<String, String> headersMap = null; Header[] headers = method.getResponseHeaders(); if (headers != null) { headersMap = new HashMap<String, String>(headers.length); for (Header header : headers) { headersMap.put(header.getName(), header.getValue()); } } return new HttpResponse(method, rq.getRunAsUser(), method.getResponseBody(), headersMap, (end - start)); } @Override public boolean onError(HttpMethod method, Throwable t) { return false; } }; HttpResponse response = null; if (rq.getPassword() != null) { response = authenticatedHttp.executeHttpMethodAuthenticated(req, rq.getRunAsUser(), rq.getPassword(), callback); } else { response = authenticatedHttp.executeHttpMethodAuthenticated(req, rq.getRunAsUser(), callback); } return response; } finally { if (req != null) { req.releaseConnection(); } } }
From source file:org.apache.cocoon.generation.WebServiceProxyGenerator.java
/** * Forwards the request and returns the response. * /* ww w. j a va 2s . co m*/ * The rest is probably out of date: * Will use a UrlGetMethod to benefit the cacheing mechanism * and intermediate proxy servers. * It is potentially possible that the size of the request * may grow beyond a certain limit for GET and it will require POST instead. * * @return byte[] XML response */ public byte[] fetch() throws ProcessingException { HttpMethod method = null; // check which method (GET or POST) to use. if (this.configuredHttpMethod.equalsIgnoreCase(METHOD_POST)) { method = new PostMethod(this.source); } else { method = new GetMethod(this.source); } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("request HTTP method: " + method.getName()); } // this should probably be exposed as a sitemap option method.setFollowRedirects(true); // copy request parameters and merge with URL parameters Request request = ObjectModelHelper.getRequest(objectModel); ArrayList paramList = new ArrayList(); Enumeration enumeration = request.getParameterNames(); while (enumeration.hasMoreElements()) { String pname = (String) enumeration.nextElement(); String[] paramsForName = request.getParameterValues(pname); for (int i = 0; i < paramsForName.length; i++) { NameValuePair pair = new NameValuePair(pname, paramsForName[i]); paramList.add(pair); } } if (paramList.size() > 0) { NameValuePair[] allSubmitParams = new NameValuePair[paramList.size()]; paramList.toArray(allSubmitParams); String urlQryString = method.getQueryString(); // use HttpClient encoding routines method.setQueryString(allSubmitParams); String submitQryString = method.getQueryString(); // set final web service query string // sometimes the querystring is null here... if (null == urlQryString) { method.setQueryString(submitQryString); } else { method.setQueryString(urlQryString + "&" + submitQryString); } } // if there are submit parameters byte[] response = null; try { int httpStatus = httpClient.executeMethod(method); if (httpStatus < 400) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Return code when accessing the remote Url: " + httpStatus); } } else { throw new ProcessingException("The remote returned error " + httpStatus + " when attempting to access remote URL:" + method.getURI()); } } catch (URIException e) { throw new ProcessingException("There is a problem with the URI: " + this.source, e); } catch (IOException e) { try { throw new ProcessingException( "Exception when attempting to access the remote URL: " + method.getURI(), e); } catch (URIException ue) { throw new ProcessingException("There is a problem with the URI: " + this.source, ue); } } finally { /* It is important to always read the entire response and release the * connection regardless of whether the server returned an error or not. * {@link http://jakarta.apache.org/commons/httpclient/tutorial.html} */ response = method.getResponseBody(); method.releaseConnection(); } return response; }
From source file:org.apache.commons.httpclient.demo.GetOtherUrlDate.java
public static void main(String[] args) { //GetOtherUrlDate getotherurldate = new GetOtherUrlDate(); HttpClient hc = new HttpClient(); ///*from www.ja v a 2s. c o m*/ //hc.getHostConfiguration().setProxy("90.0.12.21",808); HttpMethod hm = new GetMethod("http://www.sina.com.cn"); hm.addRequestHeader("Content-Type", "text/html;charset=utf-8"); // int statusCode = -1; byte[] result = null; try { statusCode = hc.executeMethod(hm); if (statusCode != HttpStatus.SC_OK) { // System.out.println("get failure!"); return; } if (hm.getResponseBody() != null) { // result = hm.getResponseBody(); //hm.getStatusLine()DDhttp } } catch (HttpException e1) { e1.printStackTrace(); } catch (java.io.IOException e2) { e2.printStackTrace(); } hm.releaseConnection(); String data = null; if (result != null) { try { //data = new String(result, "UTF-8"); // data = new String(result, "GB2312"); // } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } //System.out.println(data.substring(0, 500)); // int begin = data.indexOf("product"); // System.out.println("==============================="); System.out.println("product:" + begin); System.out.println("==============================="); if (begin > -1) { //1000 System.out.println(data.substring(begin, begin + 1000)); //System.out.println(Strings.convertHTML(data.substring(begin,begin + 1000))); } } }