Skip to content

Instantly share code, notes, and snippets.

@Narigo
Created May 10, 2013 10:50
Show Gist options
  • Select an option

  • Save Narigo/5553697 to your computer and use it in GitHub Desktop.

Select an option

Save Narigo/5553697 to your computer and use it in GitHub Desktop.
Vert.x HttpClient not working with github.io -- just put it into vertx-gradle-template and ./gradlew runMod
package com.mycompany.myproject;
/*
* Copyright 2013 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
import java.net.MalformedURLException;
import java.net.URL;
import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpClient;
import org.vertx.java.core.http.HttpClientRequest;
import org.vertx.java.core.http.HttpClientResponse;
import org.vertx.java.platform.Verticle;
/*
* This is a simple Java verticle which receives `ping` messages on the event bus and sends back
* `pong` replies
*/
public class PingVerticle extends Verticle {
public void start() {
try {
URL url = new URL(
"http://vert-x.github.com/vertx-mods/mods/com.campudus.session-manager-v1.2.1/mod.zip");
// "http://unreleased.de/stuff/mod.zip");
String host = url.getHost();
int port = url.getPort();
HttpClient client = vertx.createHttpClient().setHost(host);
if (port != -1) {
client.setPort(port);
}
HttpClientRequest req = client.get(url.getPath(), new Handler<HttpClientResponse>() {
public void handle(HttpClientResponse event) {
final Buffer buf = new Buffer(0);
event.dataHandler(new Handler<Buffer>() {
public void handle(Buffer buffer) {
buf.appendBuffer(buffer);
}
});
event.endHandler(new Handler<Void>() {
public void handle(Void event) {
System.out.println(buf.toString());
}
});
}
});
req.putHeader("Host", host);
req.putHeader("User-Agent", "Vertx client");
req.end();
} catch (MalformedURLException e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment