當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。