gleissonfreitas лет назад: 9
Родитель
Сommit
6187c0ea7d
3 измененных файлов с 112 добавлено и 0 удалено
  1. 15 0
      src/Main.java
  2. 33 0
      src/TestServletGet.java
  3. 64 0
      src/TestServletPost.java

+ 15 - 0
src/Main.java

@@ -0,0 +1,15 @@
1
+
2
+public class Main {
3
+
4
+	public static void main (String [] args){
5
+		TestServletGet c = new TestServletGet();
6
+		String test = c.getHTML("http://localhost:8080/SnailServlet/data?method=get_categories&user_id=2");
7
+		System.out.println(test);
8
+		
9
+		
10
+		String url = "http://localhost:8080/SnailServlet/user";
11
+		TestServletPost d = new TestServletPost(url);
12
+		
13
+	}
14
+
15
+}

+ 33 - 0
src/TestServletGet.java

@@ -0,0 +1,33 @@
1
+import java.io.BufferedReader;
2
+import java.io.*;
3
+import java.net.*;
4
+import java.io.IOException;
5
+
6
+public class TestServletGet {
7
+
8
+	public String getHTML(String urlToRead){
9
+		URL url;
10
+		HttpURLConnection conn;
11
+		BufferedReader rd;
12
+		String line;
13
+		StringBuilder result = new StringBuilder();
14
+		try{
15
+			url = new URL(urlToRead);
16
+			conn = (HttpURLConnection) 
17
+			url.openConnection();
18
+			conn.setRequestMethod("GET");
19
+			rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
20
+			while ((line = rd.readLine()) != null){
21
+				result.append(line);
22
+			}
23
+			rd.close();
24
+		} catch (IOException e){
25
+			e.printStackTrace();
26
+		} //catch (IOException e){
27
+			//e.printStackTrace();
28
+		//}	
29
+			return result.toString();
30
+	}
31
+}
32
+
33
+

+ 64 - 0
src/TestServletPost.java

@@ -0,0 +1,64 @@
1
+import java.io.BufferedReader;
2
+import java.io.IOException;
3
+import java.io.InputStream;
4
+import java.io.InputStreamReader;
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
8
+import org.apache.http.HttpEntity;
9
+import org.apache.http.HttpResponse;
10
+import org.apache.http.NameValuePair;
11
+import org.apache.http.client.HttpClient;
12
+import org.apache.http.client.entity.UrlEncodedFormEntity;
13
+import org.apache.http.client.methods.HttpPost;
14
+import org.apache.http.impl.client.HttpClients;
15
+import org.apache.http.message.BasicNameValuePair;
16
+
17
+
18
+public class TestServletPost{
19
+
20
+
21
+	public TestServletPost(String url){
22
+		
23
+		
24
+		InputStream instream = null;
25
+
26
+		try {
27
+			HttpClient httpclient = HttpClients.createDefault();
28
+			HttpPost httppost = new HttpPost(url);
29
+
30
+			// Request parameters and other properties.
31
+			List<NameValuePair> params = new ArrayList<NameValuePair>(2);
32
+			params.add(new BasicNameValuePair("method", "create_user"));
33
+			params.add(new BasicNameValuePair("name", "Gleisson"));
34
+			params.add(new BasicNameValuePair("bornDate", "1987/26/04"));
35
+			params.add(new BasicNameValuePair("sex", "m"));
36
+			params.add(new BasicNameValuePair("password", "SAUDE"));
37
+			params.add(new BasicNameValuePair("email", "sdrhgdsaj@sjhsdehj.com"));
38
+			httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
39
+
40
+			//Execute and get the response.
41
+			HttpResponse response = httpclient.execute(httppost);
42
+			HttpEntity entity = response.getEntity();
43
+
44
+			if (entity != null) {
45
+				 instream = entity.getContent();
46
+				 BufferedReader rd = new BufferedReader(new InputStreamReader(instream));
47
+				 String line;
48
+				while((line = rd.readLine()) != null){
49
+					System.out.println(line);
50
+					
51
+				}
52
+			}}
53
+			catch (Exception e){
54
+			}finally {
55
+				try {
56
+					instream.close();
57
+				} catch (IOException e) {
58
+					// TODO Auto-generated catch block
59
+					e.printStackTrace();
60
+				}
61
+			}
62
+			
63
+		
64
+	}}