http1


     *
     * @return HttpUtil
     */
    public static HttpTools getInstance()
    {
        return instance;
    }
   



    public InputStream sendLabelRequest(GetMethod getMethod)
        throws PortalException
    { 
        // GetMethod getMethod = new GetMethod(url);
       
        try
        {
            int resultCode = httpClient.executeMethod(getMethod);
            if (HttpStatus.SC_OK != resultCode)
            {
               
                throw new PortalException(String.valueOf(resultCode), "The response code is error!");
            }
           
            return getMethod.getResponseBodyAsStream();
        }
        catch (Exception ex)
        {
            throw new PortalException(ex);
        }
    }
   
    /**
     * 发生Get请求
     *
     * @param url 请求url
     * @return
     * @throws PortalException [参数说明]
     *
     * @return String [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public String sendHttpRequestByGet(String url)
        throws PortalException
    {
  
        GetMethod getMethod = new GetMethod(url);
       
        try
        {
            int resultCode = httpClient.executeMethod(getMethod);
            if (HttpStatus.SC_OK != resultCode)
            {
              
                throw new PortalException(String.valueOf(resultCode), "The response code is error!");
            }
           
            // 响应消息
            String responseXml = null;
            byte[] resBody = getMethod.getResponseBody();
            if (null == resBody || 0 == resBody.length)
            {
                responseXml = getMethod.getResponseBodyAsString();
            }
            else
            {
                responseXml = new String(resBody, UTF_8);
            }
            return responseXml;
        }
        catch (Exception ex)
        {
            throw new PortalException(ex);
        }
        finally
        {
            if (null != getMethod)
            {
                getMethod.releaseConnection();
            }
        }
    }
   
 
    public String sendHttpRequest(String url, String xml)
        throws PortalException
    {
        EntityEnclosingMethod httpMethod = new PostMethod(url);
       
        try
        {
            // 设置header信息,传输XML格式的
            httpMethod.setRequestHeader("content-type", "text/xml; charset=UTF-8");
           
            // 发送含xml消息体的对象
            RequestEntity entity = new StringRequestEntity(xml, "text/xml", "UTF-8");
           
            httpMethod.setRequestEntity(entity);
           
            // 处理响应结果码
            int resultCode = httpClient.executeMethod(httpMethod);
            if (HttpStatus.SC_OK != resultCode)
            {
                throw new PortalException(String.valueOf(resultCode), "The response code is error! errorCode = " + resultCode);
            }

你可能感兴趣的:(http)