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


Java GetMethod.setQueryString方法代码示例

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


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

示例1: testRedirectToResourceAfterLogout

import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
/**
 * Test SLING-1847
 * @throws Exception
 */
@Test 
public void testRedirectToResourceAfterLogout() throws Exception {
	//login
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("j_username", "admin"));
    params.add(new NameValuePair("j_password", "admin"));
    H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_MOVED_TEMPORARILY, params, null);
    
    //...and then...logout with a resource redirect
    String locationAfterLogout = HttpTest.SERVLET_CONTEXT + "/system/sling/info.sessionInfo.json";
    final GetMethod get = new GetMethod(HttpTest.HTTP_BASE_URL + "/system/sling/logout");
    NameValuePair [] logoutParams = new NameValuePair[1];
    logoutParams[0] = new NameValuePair("resource", locationAfterLogout);
    get.setQueryString(logoutParams);
    
    get.setFollowRedirects(false);
    final int status = H.getHttpClient().executeMethod(get);
    assertEquals("Expected redirect", HttpServletResponse.SC_MOVED_TEMPORARILY, status);
    Header location = get.getResponseHeader("Location");
    assertEquals(HttpTest.HTTP_BASE_URL + locationAfterLogout, location.getValue());
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:26,代码来源:RedirectOnLogoutTest.java

示例2: doGet

import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
    try{
        String item = request.getParameter("item");
        
        //in HttpClient 4.x, there is no GetMethod anymore. Instead there is HttpGet
        HttpGet httpget = new HttpGet("http://host.com?param=" + URLEncoder.encode(item)); //OK
        HttpGet httpget2 = new HttpGet("http://host.com?param=" + item); //BAD
        
        GetMethod get = new GetMethod("http://host.com?param=" + item); //BAD
        get.setQueryString("item=" + item); //BAD
        //get.execute();

    }catch(Exception e){
      System.out.println(e);
    }
}
 
开发者ID:blackarbiter,项目名称:Android_Code_Arbiter,代码行数:17,代码来源:HttpParameterPollution.java

示例3: testQueryParams

import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
public void testQueryParams() throws Exception {

        GetMethod get = new GetMethod("/");

        String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); 
        String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); 

        get.setQueryString(new NameValuePair[] {
            new NameValuePair("ru", ru_msg),
            new NameValuePair("ch", ch_msg) 
        });            

        Map params = new HashMap();
        StringTokenizer tokenizer = new StringTokenizer(
            get.getQueryString(), "&");
        while (tokenizer.hasMoreTokens()) {
            String s = tokenizer.nextToken();
            int i = s.indexOf('=');
            assertTrue("Invalid url-encoded parameters", i != -1);
            String name = s.substring(0, i).trim(); 
            String value = s.substring(i + 1, s.length()).trim(); 
            value = URIUtil.decode(value, CHARSET_UTF8);
            params.put(name, value);
        }
        assertEquals(ru_msg, params.get("ru"));
        assertEquals(ch_msg, params.get("ch"));
    }
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:28,代码来源:TestMethodCharEncoding.java

示例4: shortenUrl

import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
@Override
public String shortenUrl(String longUrl)
{
    if (log.isDebugEnabled())
    {
        log.debug("Shortening URL: " + longUrl);
    }
    String shortUrl = longUrl;
    if (longUrl.length() > urlLength)
    {
        GetMethod getMethod = new GetMethod();
        getMethod.setPath("/v3/shorten");

        List<NameValuePair> args = new ArrayList<NameValuePair>();
        args.add(new NameValuePair("login", username));
        args.add(new NameValuePair("apiKey", apiKey));
        args.add(new NameValuePair("longUrl", longUrl));
        args.add(new NameValuePair("format", "txt"));
        getMethod.setQueryString(args.toArray(new NameValuePair[args.size()]));

        try
        {
            int resultCode = httpClient.executeMethod(getMethod);
            if (resultCode == 200)
            {
                shortUrl = getMethod.getResponseBodyAsString();
            }
            else
            {
                log.warn("Failed to shorten URL " + longUrl + "  - response code == " + resultCode);
                log.warn(getMethod.getResponseBodyAsString());
            }
        }
        catch (Exception ex)
        {
            log.error("Failed to shorten URL " + longUrl, ex);
        }
        if (log.isDebugEnabled())
        {
            log.debug("URL " + longUrl + " has been shortened to " + shortUrl);
        }
    }
    return shortUrl.trim();
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:45,代码来源:BitlyUrlShortenerImpl.java

示例5: getAuthenticatedContent

import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的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 getAuthenticatedContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
    final GetMethod get = new GetMethod(url);

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

        if(params != null) {
            final NameValuePair [] nvp = new NameValuePair[0];
            get.setQueryString(params.toArray(nvp));
        }
        final int status = httpClient.executeMethod(get);
        final InputStream is = get.getResponseBodyAsStream();
        final StringBuffer content = new StringBuffer();
        final String charset = get.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 = get.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,代码行数:55,代码来源:AuthenticatedTestUtil.java


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