File tree Expand file tree Collapse file tree 6 files changed +18
-2
lines changed
javalite-common/src/main/java/org/javalite/http Expand file tree Collapse file tree 6 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ public Delete doConnect() {
39
39
try {
40
40
connection .setDoOutput (true );
41
41
connection .setRequestMethod ("DELETE" );
42
+ connection .setInstanceFollowRedirects (redirect );
42
43
connection .connect ();
43
44
return this ;
44
45
} catch (Exception e ) {
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ public Get doConnect() {
40
40
try {
41
41
connection .setDoInput (true );
42
42
connection .setDoOutput (true );
43
+ connection .setInstanceFollowRedirects (redirect );
43
44
connection .setUseCaches (false );
44
45
connection .setRequestMethod ("GET" );
45
46
return this ;
@@ -49,8 +50,7 @@ public Get doConnect() {
49
50
}
50
51
51
52
public static void main (String [] args ) {
52
-
53
- Get get = Http .get ("http://localhost:8080/manager/html" ).basic ("tomcat" , "tomcat" );
53
+ Get get = Http .get ("https://www.yahoo.com" );
54
54
System .out .println (get .text ());
55
55
System .out .println (get .headers ());
56
56
System .out .println (get .responseCode ());
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ protected Multipart doConnect() {
37
37
connection .setDoOutput (true ); // indicates POST method
38
38
connection .setDoInput (true );
39
39
connection .setRequestProperty ("Content-Type" , "multipart/form-data; boundary=" + boundary );
40
+ connection .setInstanceFollowRedirects (redirect );
40
41
outputStream = connection .getOutputStream ();
41
42
writer = new PrintWriter (new OutputStreamWriter (outputStream ), true );
42
43
sendData ();
Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ public Post doConnect() {
56
56
connection .setDoOutput (true );
57
57
connection .setUseCaches (false );
58
58
connection .setRequestMethod ("POST" );
59
+ connection .setInstanceFollowRedirects (redirect );
59
60
60
61
if (params .size () > 0 ){
61
62
connection .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ public Put doConnect() {
46
46
connection .setDoOutput (true );
47
47
connection .setDoInput (true );
48
48
connection .setRequestMethod ("PUT" );
49
+ connection .setInstanceFollowRedirects (redirect );
49
50
OutputStream os = connection .getOutputStream ();
50
51
os .write (content );
51
52
os .flush ();
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ public abstract class Request<T extends Request> {
35
35
36
36
protected final HttpURLConnection connection ;
37
37
private boolean connected ;
38
+ protected boolean redirect = false ;
38
39
protected final String url ;
39
40
40
41
public Request (String url , int connectTimeout , int readTimeout ) {
@@ -61,6 +62,17 @@ public T header(String name, String value) {
61
62
return (T ) this ;
62
63
}
63
64
65
+ /**
66
+ * Configures this request to follow redirects. Default is <code>false</code>.
67
+ *
68
+ * @see https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#instanceFollowRedirects
69
+ * @param redirect true to follow, false to not.
70
+ * @return self
71
+ */
72
+ public T redirect (boolean redirect ) {
73
+ this .redirect = redirect ;
74
+ return (T ) this ;
75
+ }
64
76
65
77
/**
66
78
* Returns input stream to read server response from.
You can’t perform that action at this time.
0 commit comments