Java Code Examples for org.apache.commons.httpclient.methods.PostMethod#getResponseHeaders()

The following examples show how to use org.apache.commons.httpclient.methods.PostMethod#getResponseHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSubmarineServerTest.java    From submarine with Apache License 2.0 6 votes vote down vote up
private static String getCookie(String user, String password) throws IOException {
  HttpClient httpClient = new HttpClient();
  PostMethod postMethod = new PostMethod(URL + "/login");
  postMethod.addRequestHeader("Origin", URL);
  postMethod.setParameter("password", password);
  postMethod.setParameter("userName", user);
  httpClient.executeMethod(postMethod);
  LOG.info("{} - {}", postMethod.getStatusCode(), postMethod.getStatusText());
  Pattern pattern = Pattern.compile("JSESSIONID=([a-zA-Z0-9-]*)");
  Header[] setCookieHeaders = postMethod.getResponseHeaders("Set-Cookie");
  String jsessionId = null;
  for (Header setCookie : setCookieHeaders) {
    java.util.regex.Matcher matcher = pattern.matcher(setCookie.toString());
    if (matcher.find()) {
      jsessionId = matcher.group(1);
    }
  }

  if (jsessionId != null) {
    return jsessionId;
  } else {
    return StringUtils.EMPTY;
  }
}
 
Example 2
Source File: AbstractTestRestApi.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private static String getCookie(String user, String password) throws IOException {
  HttpClient httpClient = new HttpClient();
  PostMethod postMethod = new PostMethod(URL + "/login");
  postMethod.addRequestHeader("Origin", URL);
  postMethod.setParameter("password", password);
  postMethod.setParameter("userName", user);
  httpClient.executeMethod(postMethod);
  LOG.info("{} - {}", postMethod.getStatusCode(), postMethod.getStatusText());
  Pattern pattern = Pattern.compile("JSESSIONID=([a-zA-Z0-9-]*)");
  Header[] setCookieHeaders = postMethod.getResponseHeaders("Set-Cookie");
  String jsessionId = null;
  for (Header setCookie : setCookieHeaders) {
    java.util.regex.Matcher matcher = pattern.matcher(setCookie.toString());
    if (matcher.find()) {
      jsessionId = matcher.group(1);
    }
  }

  if (jsessionId != null) {
    return jsessionId;
  } else {
    return StringUtils.EMPTY;
  }
}
 
Example 3
Source File: ZeppelinServerMock.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private static String getCookie(String user, String password) throws IOException {
  HttpClient httpClient = new HttpClient();
  PostMethod postMethod = new PostMethod(URL + "/login");
  postMethod.addRequestHeader("Origin", URL);
  postMethod.setParameter("password", password);
  postMethod.setParameter("userName", user);
  httpClient.executeMethod(postMethod);
  LOG.info("{} - {}", postMethod.getStatusCode(), postMethod.getStatusText());
  Pattern pattern = Pattern.compile("JSESSIONID=([a-zA-Z0-9-]*)");
  Header[] setCookieHeaders = postMethod.getResponseHeaders("Set-Cookie");
  String jsessionId = null;
  for (Header setCookie : setCookieHeaders) {
    java.util.regex.Matcher matcher = pattern.matcher(setCookie.toString());
    if (matcher.find()) {
      jsessionId = matcher.group(1);
    }
  }

  if (jsessionId != null) {
    return jsessionId;
  } else {
    return StringUtils.EMPTY;
  }
}
 
Example 4
Source File: UserController.java    From OfficeAutomatic-System with Apache License 2.0 5 votes vote down vote up
private void message(String phone,String hysbh,String time) throws IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
    post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
    NameValuePair[] data = { new NameValuePair("Uid", "dddz97"),
            new NameValuePair("Key", "d41d8cd98f00b204e980"),
            new NameValuePair("smsMob", phone),
            new NameValuePair("smsText", "请于"+time+",到"+hysbh+"开会") };
    post.setRequestBody(data);

    client.executeMethod(post);
    Header[] headers = post.getResponseHeaders();
    int statusCode = post.getStatusCode();
    System.out.println("statusCode:" + statusCode);
    for (Header h : headers) {
        System.out.println(h.toString());
    }
    String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
    System.out.println(result); // 打印返回消息状态

    post.releaseConnection();
}
 
Example 5
Source File: RemoteLogRepositoryBackend.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
public void upLoadFile(LogFile file, long ticketId)
{
    if(!isConnected())
    {
        PacketSamurai.getUserInterface().log("You have to be connected to use the Remote Log Repository");
        return;
    }
    try
    {
        PostMethod filePost = new PostMethod(_repoLocation+_repoTicketScript);
        Part[] parts = 
        { 
                new StringPart("user", this.getUserName()),
                new StringPart("pass", this.getPassword()),
                new StringPart("id", Long.toString(ticketId)),
                new StringPart("req[comment]", file.getComments()),
                new StringPart("req[analyzerflags]", Long.toString(file.getAnalyserBitSet())),
                new StringPart("req[servertype]", file.getServerType()),
                new StringPart("req[protocol]", file.getProtocolName()),
                new LogFilePart(file.getName(), file)
        };
        filePost.setRequestEntity( new MultipartRequestEntity(parts, filePost.getParams()) );
        System.out.println("a");
        int response = _httpClient.executeMethod(filePost);
        System.out.println("b");
        if (response != HttpStatus.SC_OK)
        {
            System.out.println("HTTP "+response);
            System.out.println(filePost.getResponseBodyAsString());
        }
        else
        {
            file.setRemote(true);
            filePost.getResponseHeaders();
            System.out.println("OK");
            System.out.println(filePost.getResponseBodyAsString());

        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
 
Example 6
Source File: KafkaAvroSchemaRegistry.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Register a schema to the Kafka schema registry
 *
 * @param schema
 * @return schema ID of the registered schema
 * @throws SchemaRegistryException if registration failed
 */
@Override
public synchronized String register(Schema schema) throws SchemaRegistryException {

  // Change namespace if override specified
  if (this.namespaceOverride.isPresent()) {
    schema = AvroUtils.switchNamespace(schema, this.namespaceOverride.get());
  }

  LOG.info("Registering schema " + schema.toString());

  PostMethod post = new PostMethod(url);
  post.addParameter("schema", schema.toString());

  HttpClient httpClient = this.borrowClient();
  try {
    LOG.debug("Loading: " + post.getURI());
    int statusCode = httpClient.executeMethod(post);
    if (statusCode != HttpStatus.SC_CREATED) {
      throw new SchemaRegistryException("Error occurred while trying to register schema: " + statusCode);
    }

    String response;
    response = post.getResponseBodyAsString();
    if (response != null) {
      LOG.info("Received response " + response);
    }

    String schemaKey;
    Header[] headers = post.getResponseHeaders(SCHEMA_ID_HEADER_NAME);
    if (headers.length != 1) {
      throw new SchemaRegistryException(
          "Error reading schema id returned by registerSchema call: headers.length = " + headers.length);
    } else if (!headers[0].getValue().startsWith(SCHEMA_ID_HEADER_PREFIX)) {
      throw new SchemaRegistryException(
          "Error parsing schema id returned by registerSchema call: header = " + headers[0].getValue());
    } else {
      LOG.info("Registered schema successfully");
      schemaKey = headers[0].getValue().substring(SCHEMA_ID_HEADER_PREFIX.length());
    }

    return schemaKey;
  } catch (Throwable t) {
    throw new SchemaRegistryException(t);
  } finally {
    post.releaseConnection();
    this.httpClientPool.returnObject(httpClient);
  }
}
 
Example 7
Source File: LiKafkaSchemaRegistry.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Register a schema to the Kafka schema registry
 *
 * @param schema
 * @param post
 * @return schema ID of the registered schema
 * @throws SchemaRegistryException if registration failed
 */
public synchronized MD5Digest register(Schema schema, PostMethod post) throws SchemaRegistryException {

  // Change namespace if override specified
  if (this.namespaceOverride.isPresent()) {
    schema = AvroUtils.switchNamespace(schema, this.namespaceOverride.get());
  }

  LOG.info("Registering schema " + schema.toString());

  post.addParameter("schema", schema.toString());

  HttpClient httpClient = this.borrowClient();
  try {
    LOG.debug("Loading: " + post.getURI());
    int statusCode = httpClient.executeMethod(post);
    if (statusCode != HttpStatus.SC_CREATED) {
      throw new SchemaRegistryException("Error occurred while trying to register schema: " + statusCode);
    }

    String response;
    response = post.getResponseBodyAsString();
    if (response != null) {
      LOG.info("Received response " + response);
    }

    String schemaKey;
    Header[] headers = post.getResponseHeaders(SCHEMA_ID_HEADER_NAME);
    if (headers.length != 1) {
      throw new SchemaRegistryException(
          "Error reading schema id returned by registerSchema call: headers.length = " + headers.length);
    } else if (!headers[0].getValue().startsWith(SCHEMA_ID_HEADER_PREFIX)) {
      throw new SchemaRegistryException(
          "Error parsing schema id returned by registerSchema call: header = " + headers[0].getValue());
    } else {
      LOG.info("Registered schema successfully");
      schemaKey = headers[0].getValue().substring(SCHEMA_ID_HEADER_PREFIX.length());
    }
    MD5Digest schemaId = MD5Digest.fromString(schemaKey);
    return schemaId;
  } catch (Throwable t) {
    throw new SchemaRegistryException(t);
  } finally {
    post.releaseConnection();
    this.httpClientPool.returnObject(httpClient);
  }
}