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