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


Java PostMethod.setDoAuthentication方法代码示例

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


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

示例1: assertAuthenticatedPostStatus

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
/** Execute a POST request and check status */
public void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode, List<NameValuePair> postParams, String assertMessage)
throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);

    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);
        if(assertMessage == null) {
            assertEquals(expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:29,代码来源:AuthenticatedTestUtil.java

示例2: makeRestCallPost

import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
public MirrorGateResponse makeRestCallPost(String url, String jsonString, String user, String password) {

        MirrorGateResponse response;
        PostMethod post = new PostMethod(url);
        try {
            HttpClient client = getHttpClient();

            if (user != null && password != null) {
                client.getState().setCredentials(
                        AuthScope.ANY,
                        new UsernamePasswordCredentials(user, password));
                post.setDoAuthentication(true);
            }

            StringRequestEntity requestEntity = new StringRequestEntity(
                    jsonString,
                    "application/json",
                    "UTF-8");
            post.setRequestEntity(requestEntity);
            int responseCode = client.executeMethod(post);
            String responseString = post.getResponseBodyAsStream() != null ?
                    getResponseString(post.getResponseBodyAsStream()) : "";
            response = new MirrorGateResponse(responseCode, responseString);
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "MirrorGate: Error posting to MirrorGate", e);
            response = new MirrorGateResponse(HttpStatus.SC_BAD_REQUEST, "");
        } finally {
            post.releaseConnection();
        }
        return response;
    }
 
开发者ID:BBVA,项目名称:mirrorgate-jenkins-builds-collector,代码行数:32,代码来源:RestCall.java

示例3: 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.setDoAuthentication方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。