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


Java PostMethod.getResponseHeader方法代码示例

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


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

示例1: runTest

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
private void runTest(String acceptHeaderValue, boolean useHttpEquiv, String expectedContentType) throws Exception {
    final String info = (useHttpEquiv ? "Using http-equiv parameter" : "Using Accept header") + ": ";
    final String url = HTTP_BASE_URL + MY_TEST_PATH;
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    
    if(acceptHeaderValue != null) {
        if(useHttpEquiv) {
            post.addParameter(":http-equiv-accept", acceptHeaderValue);
        } else {
            post.addRequestHeader("Accept", acceptHeaderValue);
        }
    }
    
    final int status = httpClient.executeMethod(post) / 100;
    assertEquals(info + "Expected status 20x for POST at " + url, 2, status);
    final Header h = post.getResponseHeader("Content-Type");
    assertNotNull(info + "Expected Content-Type header", h);
    final String ct = h.getValue();
    assertTrue(info + "Expected Content-Type '" + expectedContentType + "' for Accept header=" + acceptHeaderValue
            + " but got '" + ct + "'",
            ct.startsWith(expectedContentType));
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:24,代码来源:PostServletOutputContentTypeTest.java

示例2: testRedirectToLoginFormAfterLoginError

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
/**
   * Test SLING-2165.  Login Error should redirect back to the referrer
   * login page.
   *
   * @throws Exception
   */
  public void testRedirectToLoginFormAfterLoginError() throws Exception {
  	//login failure
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new NameValuePair("j_username", "___bogus___"));
      params.add(new NameValuePair("j_password", "not_a_real_user"));
      final String loginPageUrl = String.format("%s/system/sling/form/login", HTTP_BASE_URL);
PostMethod post = (PostMethod)assertPostStatus(HTTP_BASE_URL + "/j_security_check",
      		HttpServletResponse.SC_MOVED_TEMPORARILY,
      		params,
      		null,
      		loginPageUrl);

      final Header locationHeader = post.getResponseHeader("Location");
      String location = locationHeader.getValue();
      int queryStrStart = location.indexOf('?');
      if (queryStrStart != -1) {
      	location = location.substring(0, queryStrStart);
      }
      assertEquals("Expected to remain on the form/login page", loginPageUrl, location);
  }
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:27,代码来源:RedirectOnLoginErrorTest.java

示例3: getPostResponseHeader

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
public static String getPostResponseHeader(String url,String argJson,List<UHeader> headerList,String headerName){
  	String info = "";
  	try {
   	HttpClient client = new HttpClient();
	PostMethod method = new PostMethod(url);
	client.getParams().setContentCharset("UTF-8");
	if(headerList.size()>0){
		for(int i = 0;i<headerList.size();i++){
			UHeader header = headerList.get(i);
			method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());
		}
	}
	method.getParams().setParameter(
			HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
	if(argJson != null && !argJson.trim().equals("")) {
		RequestEntity requestEntity = new StringRequestEntity(argJson,"application/json","UTF-8");
		method.setRequestEntity(requestEntity);
	}
	method.releaseConnection();
	Header h =  method.getResponseHeader(headerName);
	info = h.getValue();
} catch (IOException e) {
	e.printStackTrace();
}
  	return info;
  }
 
开发者ID:noseparte,项目名称:Spring-Boot-Server,代码行数:27,代码来源:HttpUtils.java

示例4: doPost

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
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,代码行数:38,代码来源:HttpRequest.java

示例5: isGzipResponse

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
/**
 * Determine whether the given response is a GZIP response.
 * <p>Default implementation checks whether the HTTP "Content-Encoding"
 * header contains "gzip" (in any casing).
 * @param postMethod the PostMethod to check
 */
protected boolean isGzipResponse(PostMethod postMethod) {
	Header encodingHeader = postMethod.getResponseHeader(HTTP_HEADER_CONTENT_ENCODING);
	if (encodingHeader == null || encodingHeader.getValue() == null) {
		return false;
	}
	return (encodingHeader.getValue().toLowerCase().indexOf(ENCODING_GZIP) != -1);
}
 
开发者ID:ITYug,项目名称:kaltura-ce-sakai-extension,代码行数:14,代码来源:KalturaClientBase.java

示例6: postQuery

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
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,代码行数:50,代码来源:SolrQueryHTTPClient.java

示例7: getAuthenticatedPostContent

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
/** retrieve the contents of given URL and assert its content type
  * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
  * @throws IOException
  * @throws HttpException */
 public String getAuthenticatedPostContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> postParams, int expectedStatusCode) throws IOException {
     final PostMethod post = new PostMethod(url);

     URL baseUrl = new URL(HTTP_BASE_URL);
     AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
     post.setDoAuthentication(true);
     Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
 	try {
httpClient.getState().setCredentials(authScope, creds);

      if(postParams!=null) {
          final NameValuePair [] nvp = {};
          post.setRequestBody(postParams.toArray(nvp));
      }

      final int status = httpClient.executeMethod(post);
      final InputStream is = post.getResponseBodyAsStream();
      final StringBuffer content = new StringBuffer();
      final String charset = post.getResponseCharSet();
      final byte [] buffer = new byte[16384];
      int n = 0;
      while( (n = is.read(buffer, 0, buffer.length)) > 0) {
          content.append(new String(buffer, 0, n, charset));
      }
      assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")",
              expectedStatusCode,status);
      final Header h = post.getResponseHeader("Content-Type");
      if(expectedContentType == null) {
          if(h!=null) {
              fail("Expected null Content-Type, got " + h.getValue());
          }
      } else if(CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
          // no check
      } else if(h==null) {
          fail(
                  "Expected Content-Type that starts with '" + expectedContentType
                  +" but got no Content-Type header at " + url
          );
      } else {
          assertTrue(
              "Expected Content-Type that starts with '" + expectedContentType
              + "' for " + url + ", got '" + h.getValue() + "'",
              h.getValue().startsWith(expectedContentType)
          );
      }
      return content.toString();

 	} finally {
     	httpClient.getState().setCredentials(authScope, oldCredentials);
 	}
 }
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:56,代码来源:AuthenticatedTestUtil.java


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