Search in sources :

Example 11 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project camel by apache.

the class NettyHttpMapHeadersFalseTest method testHttpHeaderCase.

@Test
public void testHttpHeaderCase() throws Exception {
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
    method.setRequestHeader("clientHeader", "fooBAR");
    method.setRequestHeader("OTHER", "123");
    method.setRequestHeader("beer", "Carlsberg");
    client.executeMethod(method);
    assertEquals("Bye World", method.getResponseBodyAsString());
    assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue());
    assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Example 12 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project zaproxy by zaproxy.

the class HttpSender method send.

private void send(HttpMessage msg, boolean isFollowRedirect) throws IOException {
    HttpMethod method = null;
    HttpResponseHeader resHeader = null;
    try {
        method = runMethod(msg, isFollowRedirect);
        // successfully executed;
        resHeader = HttpMethodHelper.getHttpResponseHeader(method);
        // replaceAll("Transfer-Encoding: chunked\r\n",
        resHeader.setHeader(HttpHeader.TRANSFER_ENCODING, null);
        // "");
        msg.setResponseHeader(resHeader);
        msg.getResponseBody().setCharset(resHeader.getCharset());
        msg.getResponseBody().setLength(0);
        // ZAP: Moreover do not set content length to zero
        if (!msg.isEventStream()) {
            msg.getResponseBody().append(method.getResponseBody());
        }
        msg.setResponseFromTargetHost(true);
        // ZAP: set method to retrieve upgraded channel later
        if (method instanceof ZapGetMethod) {
            msg.setUserObject(method);
        }
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}
Also used : ZapGetMethod(org.zaproxy.zap.ZapGetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 13 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project zaproxy by zaproxy.

the class HttpSender method runMethod.

private HttpMethod runMethod(HttpMessage msg, boolean isFollowRedirect) throws IOException {
    HttpMethod method = null;
    // no more retry
    modifyUserAgent(msg);
    method = helper.createRequestMethod(msg.getRequestHeader(), msg.getRequestBody());
    if (!(method instanceof EntityEnclosingMethod)) {
        // cant do this for EntityEnclosingMethod methods - it will fail
        method.setFollowRedirects(isFollowRedirect);
    }
    // ZAP: Use custom HttpState if needed
    User forceUser = this.getUser(msg);
    if (forceUser != null) {
        this.executeMethod(method, forceUser.getCorrespondingHttpState());
    } else {
        this.executeMethod(method, null);
    }
    HttpMethodHelper.updateHttpRequestHeaderSent(msg.getRequestHeader(), method);
    return method;
}
Also used : User(org.zaproxy.zap.users.User) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 14 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project zaproxy by zaproxy.

the class HttpMethodHelper method createRequestMethod.

//  This is the currently in use method.
// may be replaced by the New method - however the New method is not yet fully tested so this is stil used.
public HttpMethod createRequestMethod(HttpRequestHeader header, HttpBody body) throws URIException {
    HttpMethod httpMethod = null;
    String method = header.getMethod();
    URI uri = header.getURI();
    String version = header.getVersion();
    if (method == null || method.trim().length() < 3) {
        throw new URIException("Invalid HTTP method: " + method);
    }
    if (method.equalsIgnoreCase(GET)) {
        //httpMethod = new GetMethod();
        // ZAP: avoid discarding HTTP status code 101 that is used for WebSocket upgrade 
        httpMethod = new ZapGetMethod();
    } else if (method.equalsIgnoreCase(POST)) {
        httpMethod = new ZapPostMethod();
    } else if (method.equalsIgnoreCase(DELETE)) {
        httpMethod = new ZapDeleteMethod();
    } else if (method.equalsIgnoreCase(PUT)) {
        httpMethod = new ZapPutMethod();
    } else if (method.equalsIgnoreCase(HEAD)) {
        httpMethod = new ZapHeadMethod();
    } else if (method.equalsIgnoreCase(OPTIONS)) {
        httpMethod = new ZapOptionsMethod();
    } else if (method.equalsIgnoreCase(TRACE)) {
        httpMethod = new ZapTraceMethod(uri.toString());
    } else {
        httpMethod = new GenericMethod(method);
    }
    try {
        httpMethod.setURI(uri);
    } catch (Exception e1) {
        throw new URIException("Failed to set URI [" + uri + "]: " + e1.getMessage());
    }
    HttpMethodParams httpParams = httpMethod.getParams();
    // default to use HTTP 1.0
    httpParams.setVersion(HttpVersion.HTTP_1_0);
    if (version.equalsIgnoreCase(HttpHeader.HTTP11)) {
        httpParams.setVersion(HttpVersion.HTTP_1_1);
    }
    // set various headers
    int pos = 0;
    // ZAP: changed to always use CRLF, like the HttpHeader
    Pattern pattern = patternCRLF;
    String delimiter = header.getLineDelimiter();
    // ZAP: Shouldn't happen as the HttpHeader always uses CRLF
    if (delimiter.equals(LF)) {
        delimiter = LF;
        pattern = patternLF;
    }
    String msg = header.getHeadersAsString();
    String[] split = pattern.split(msg);
    String token = null;
    String name = null;
    String value = null;
    for (int i = 0; i < split.length; i++) {
        token = split[i];
        if (token.equals("")) {
            continue;
        }
        if ((pos = token.indexOf(":")) < 0) {
            return null;
        }
        name = token.substring(0, pos).trim();
        value = token.substring(pos + 1).trim();
        httpMethod.addRequestHeader(name, value);
    }
    // set body if post method or put method
    if (body != null && body.length() > 0 && (httpMethod instanceof EntityEnclosingMethod)) {
        EntityEnclosingMethod post = (EntityEnclosingMethod) httpMethod;
        //			post.setRequestEntity(new StringRequestEntity(body.toString()));
        post.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
    }
    httpMethod.setFollowRedirects(false);
    return httpMethod;
}
Also used : ZapPutMethod(org.zaproxy.zap.network.ZapPutMethod) Pattern(java.util.regex.Pattern) ZapGetMethod(org.zaproxy.zap.ZapGetMethod) ZapTraceMethod(org.zaproxy.zap.network.ZapTraceMethod) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) ZapOptionsMethod(org.zaproxy.zap.network.ZapOptionsMethod) ZapHeadMethod(org.zaproxy.zap.network.ZapHeadMethod) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) URI(org.apache.commons.httpclient.URI) URIException(org.apache.commons.httpclient.URIException) URIException(org.apache.commons.httpclient.URIException) ZapPostMethod(org.zaproxy.zap.network.ZapPostMethod) ZapDeleteMethod(org.zaproxy.zap.network.ZapDeleteMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 15 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project tdi-studio-se by Talend.

the class MDMTransaction method commit.

public void commit() throws IOException {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    HttpMethod method = new PostMethod(url + "/" + id);
    method.setDoAuthentication(true);
    try {
        //$NON-NLS-1$ //$NON-NLS-2$
        method.setRequestHeader("Cookie", getStickySession() + "=" + sessionId);
        client.executeMethod(method);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        method.releaseConnection();
    }
    int statuscode = method.getStatusCode();
    if (statuscode >= 400) {
        throw new MDMTransactionException("Commit failed. The commit operation has returned the code " + statuscode + ".");
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

HttpMethod (org.apache.commons.httpclient.HttpMethod)151 HttpClient (org.apache.commons.httpclient.HttpClient)99 GetMethod (org.apache.commons.httpclient.methods.GetMethod)95 InputStream (java.io.InputStream)61 IOException (java.io.IOException)43 ArrayList (java.util.ArrayList)30 HttpException (org.apache.commons.httpclient.HttpException)28 Map (java.util.Map)24 Test (org.junit.Test)23 Element (org.w3c.dom.Element)22 HashMap (java.util.HashMap)20 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 Header (org.apache.commons.httpclient.Header)17 List (java.util.List)14 NameValuePair (org.apache.commons.httpclient.NameValuePair)13 NodeList (org.w3c.dom.NodeList)12 FileInputStream (java.io.FileInputStream)10 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)10 SAXBuilder (org.jdom.input.SAXBuilder)10 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9