Skip to content

Commit 8ff25b0

Browse files
authored
they overcame him by the blood of the Lamb
And they overcame him by the blood of the Lamb, and by the word of their testimony; and they loved not their lives unto the death. (Revelation 12:11)
1 parent df3488b commit 8ff25b0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
//And they overcame him by the blood of the Lamb, and by the word of their testimony; and they loved not their lives unto the death. (Revelation 12:11)
3+
4+
package com.javarush.task.task40.task4002;
5+
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.NameValuePair;
8+
import org.apache.http.client.HttpClient;
9+
import org.apache.http.client.entity.UrlEncodedFormEntity;
10+
import org.apache.http.client.methods.HttpPost;
11+
import org.apache.http.impl.client.HttpClientBuilder;
12+
import org.apache.http.message.BasicNameValuePair;
13+
import java.io.BufferedReader;
14+
import java.io.InputStreamReader;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
/*
19+
Опять POST, а не GET
20+
*/
21+
22+
public class Solution {
23+
public static void main(String[] args) throws Exception {
24+
Solution solution = new Solution();
25+
solution.sendPost("http://requestb.in/tu466itu", "name=zapp&mood=good&locale=&id=777");
26+
}
27+
28+
public void sendPost(String url, String urlParameters) throws Exception {
29+
HttpClient client = getHttpClient();
30+
HttpPost request = new HttpPost(url);
31+
request.addHeader("User-Agent", "Mozilla/5.0");
32+
33+
List<NameValuePair> valuePairs = new ArrayList<NameValuePair>();
34+
String[] s = urlParameters.split("&");
35+
for (int i = 0; i < s.length; i++) {
36+
String g = s[i];
37+
valuePairs.add(new BasicNameValuePair(g.substring(0,g.indexOf("=")), g.substring(g.indexOf("=")+1)));
38+
}
39+
40+
request.setEntity(new UrlEncodedFormEntity(valuePairs));
41+
HttpResponse response = client.execute(request);
42+
System.out.println("Response Code: " + response.getStatusLine().getStatusCode());
43+
44+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
45+
StringBuffer result = new StringBuffer();
46+
String responseLine;
47+
while ((responseLine = bufferedReader.readLine()) != null) {
48+
result.append(responseLine);
49+
}
50+
51+
System.out.println("Response: " + result.toString());
52+
}
53+
54+
protected HttpClient getHttpClient() {
55+
return HttpClientBuilder.create().build();
56+
}
57+
}
58+
59+
/*
60+
Опять POST, а не GET
61+
62+
Исправь ошибки в методе sendPost, чтобы он отправлял POST-запрос с переданными параметрами.
63+
64+
Примечание: метод main в тестировании не участвует, но чтобы программа корректно работала локально, можешь зайти на сайт http://requestb.in/, создать свой RequestBin и использовать его в main.
65+
66+
67+
68+
69+
70+
Требования:
71+
72+
1. Метод sendPost должен создавать объект типа HttpPost с параметром url.
73+
74+
2. Метод sendPost должен вызвать метод setEntity у созданного объекта типа HttpPost.
75+
76+
3. В OutputStream соединения должны быть записаны переданные в метод sendPost параметры.
77+
78+
4. Метод sendPost должен использовать метод getHttpClient для получения HttpClient.
79+
*/

0 commit comments

Comments
 (0)