当前位置: 首页>>代码示例>>Java>>正文


Java HttpStatus.SC_MOVED_PERMANENTLY属性代码示例

本文整理汇总了Java中org.apache.commons.httpclient.HttpStatus.SC_MOVED_PERMANENTLY属性的典型用法代码示例。如果您正苦于以下问题:Java HttpStatus.SC_MOVED_PERMANENTLY属性的具体用法?Java HttpStatus.SC_MOVED_PERMANENTLY怎么用?Java HttpStatus.SC_MOVED_PERMANENTLY使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在org.apache.commons.httpclient.HttpStatus的用法示例。


在下文中一共展示了HttpStatus.SC_MOVED_PERMANENTLY属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: isRedirect

private boolean isRedirect(HttpMethod method)
{
    switch (method.getStatusCode()) {
    case HttpStatus.SC_MOVED_TEMPORARILY:
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_SEE_OTHER:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
        if (method.getFollowRedirects()) {
            return true;
        } else {
            return false;
        }
    default:
        return false;
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-core,代码行数:16,代码来源:AbstractHttpClient.java

示例2: doPost

public String doPost(String url, String charset, String jsonObj) {
    String resStr = null;
    HttpClient htpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    postMethod.getParams().setParameter(
            HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
    try {
        postMethod.setRequestEntity(new StringRequestEntity(jsonObj,
                "application/json", charset));
        int statusCode = htpClient.executeMethod(postMethod);
        if (statusCode != HttpStatus.SC_OK) {
            // post和put不能自动处理转发 301:永久重定向,告诉客户端以后应从新地址访问 302:Moved
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = postMethod
                        .getResponseHeader("location");
                String location = null;
                if (locationHeader != null) {
                    location = locationHeader.getValue();
                    log.info("The page was redirected to :" + location);
                } else {
                    log.info("Location field value is null");
                }
            } else {
                log.error("Method failed: " + postMethod.getStatusLine());
            }
            return resStr;
        }
        byte[] responseBody = postMethod.getResponseBody();
        resStr = new String(responseBody, charset);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        postMethod.releaseConnection();
    }
    return resStr;
}
 
开发者ID:BriData,项目名称:DBus,代码行数:37,代码来源:HttpRequest.java

示例3: verifyResponseCode

/**
 * @param responseCode
 * @return
 */
public static boolean verifyResponseCode(final int responseCode) {
    switch (responseCode) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
            return true;
        default:
            return false;
    }
}
 
开发者ID:MissionCriticalCloud,项目名称:cosmic,代码行数:14,代码来源:HTTPUtils.java

示例4: postQuery

protected JSONObject postQuery(HttpClient httpClient, String url, JSONObject body) throws UnsupportedEncodingException,
            IOException, HttpException, URIException, JSONException
{
    PostMethod post = new PostMethod(url);
    if (body.toString().length() > DEFAULT_SAVEPOST_BUFFER)
    {
        post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    }
    post.setRequestEntity(new ByteArrayRequestEntity(body.toString().getBytes("UTF-8"), "application/json"));

    try
    {
        httpClient.executeMethod(post);

        if(post.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || post.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
        {
            Header locationHeader = post.getResponseHeader("location");
            if (locationHeader != null)
            {
                String redirectLocation = locationHeader.getValue();
                post.setURI(new URI(redirectLocation, true));
                httpClient.executeMethod(post);
            }
        }

        if (post.getStatusCode() != HttpServletResponse.SC_OK)
        {
            throw new LuceneQueryParserException("Request failed " + post.getStatusCode() + " " + url.toString());
        }

        Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
        // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
        JSONObject json = new JSONObject(new JSONTokener(reader));

        if (json.has("status"))
        {
            JSONObject status = json.getJSONObject("status");
            if (status.getInt("code") != HttpServletResponse.SC_OK)
            {
                throw new LuceneQueryParserException("SOLR side error: " + status.getString("message"));
            }
        }
        return json;
    }
    finally
    {
        post.releaseConnection();
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:49,代码来源:SolrQueryHTTPClient.java

示例5: doPost

/**
 * POST请求
 *
 * @param url           请求url
 * @param paramsMap     请求参数MAP
 * @param jsonXMLString body json字符串
 * @return
 */
public static String doPost(String url, Map<String, String> paramsMap, String jsonXMLString) {
    HttpPost httpPost = new HttpPost(url);
    // set header
    setHeader(httpPost, url);

    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SO_TIME_OUT).setConnectTimeout(CONN_TIME_OUT).setConnectionRequestTimeout(REQUEST_TIME_OUT).setExpectContinueEnabled(false).build();

    // RequestConfig.DEFAULT
    httpPost.setConfig(requestConfig);

    // 响应内容
    String responseContent = null;
    String strRep = null;
    ThreadLocal<CloseableHttpClient> httpClient = new ThreadLocal<CloseableHttpClient>();
    try {
        if (paramsMap != null && jsonXMLString == null) {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(getParamsList(paramsMap), "UTF-8");
            httpPost.setEntity(entity);
        }
        else {
            httpPost.setEntity(new StringEntity(jsonXMLString, "UTF-8"));
        }

        // 执行post请求
        CloseableHttpClient client = HttpConnectionManager.getHttpClient();
        httpClient.set(client);
        HttpResponse httpResponse = httpClient.get().execute(httpPost);

        // 获取响应消息实体
        HttpEntity entityRep = httpResponse.getEntity();
        if (entityRep != null) {
            responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

            // 获取HTTP响应的状态码
            int statusCode = httpResponse.getStatusLine().getStatusCode();

            if (statusCode == HttpStatus.SC_OK) {
                strRep = responseContent;
            }
            else if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statusCode == HttpStatus.SC_SEE_OTHER)
                    || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
            }
            // Consume response content
            EntityUtils.consume(entityRep);
            // Do not need the rest
            httpPost.abort();
        }
    } catch (Exception e) {
        log.error("POST请求发生系统异常:", e);
    } finally {
        httpPost.releaseConnection();
    }
    return strRep;
}
 
开发者ID:xmomen,项目名称:dms-webapp,代码行数:64,代码来源:HttpUtils.java


注:本文中的org.apache.commons.httpclient.HttpStatus.SC_MOVED_PERMANENTLY属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。