本文整理汇总了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;
}
}
示例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;
}
示例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;
}
}
示例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();
}
}
示例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;
}