Merge branch 'master' into master
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.curltojava;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaCurlExamples {
|
||||
|
||||
public static String inputStreamToString(InputStream inputStream) {
|
||||
final int bufferSize = 8 * 1024;
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, bufferSize)) {
|
||||
while (bufferedInputStream.read(buffer) != -1) {
|
||||
builder.append(new String(buffer));
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(JavaCurlExamples.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void consumeInputStream(InputStream inputStream) {
|
||||
inputStreamToString(inputStream);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.httprequest;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class FullResponseBuilder {
|
||||
public static String getFullResponse(HttpURLConnection con) throws IOException {
|
||||
StringBuilder fullResponseBuilder = new StringBuilder();
|
||||
|
||||
fullResponseBuilder.append(con.getResponseCode())
|
||||
.append(" ")
|
||||
.append(con.getResponseMessage())
|
||||
.append("\n");
|
||||
|
||||
con.getHeaderFields()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getKey() != null)
|
||||
.forEach(entry -> {
|
||||
|
||||
fullResponseBuilder.append(entry.getKey())
|
||||
.append(": ");
|
||||
|
||||
List<String> headerValues = entry.getValue();
|
||||
Iterator<String> it = headerValues.iterator();
|
||||
if (it.hasNext()) {
|
||||
fullResponseBuilder.append(it.next());
|
||||
|
||||
while (it.hasNext()) {
|
||||
fullResponseBuilder.append(", ")
|
||||
.append(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
fullResponseBuilder.append("\n");
|
||||
});
|
||||
|
||||
Reader streamReader = null;
|
||||
|
||||
if (con.getResponseCode() > 299) {
|
||||
streamReader = new InputStreamReader(con.getErrorStream());
|
||||
} else {
|
||||
streamReader = new InputStreamReader(con.getInputStream());
|
||||
}
|
||||
|
||||
BufferedReader in = new BufferedReader(streamReader);
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
fullResponseBuilder.append("Response: ")
|
||||
.append(content);
|
||||
|
||||
return fullResponseBuilder.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.httprequest;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParameterStringBuilder {
|
||||
public static String getParamsString(Map<String, String> params) throws UnsupportedEncodingException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
|
||||
result.append("=");
|
||||
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
||||
result.append("&");
|
||||
}
|
||||
|
||||
String resultString = result.toString();
|
||||
return resultString.length() > 0 ? resultString.substring(0, resultString.length() - 1) : resultString;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.mail;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import java.io.File;
|
||||
import java.util.Properties;
|
||||
|
||||
public class EmailService {
|
||||
|
||||
private String host = "";
|
||||
private int port = 0;
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
|
||||
|
||||
public EmailService(String host, int port, String username, String password) {
|
||||
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
|
||||
sendMail();
|
||||
}
|
||||
|
||||
private void sendMail() {
|
||||
|
||||
Properties prop = new Properties();
|
||||
prop.put("mail.smtp.auth", true);
|
||||
prop.put("mail.smtp.starttls.enable", "true");
|
||||
prop.put("mail.smtp.host", host);
|
||||
prop.put("mail.smtp.port", port);
|
||||
prop.put("mail.smtp.ssl.trust", host);
|
||||
|
||||
Session session = Session.getInstance(prop, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress("from@gmail.com"));
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@gmail.com"));
|
||||
message.setSubject("Mail Subject");
|
||||
|
||||
String msg = "This is my first email using JavaMailer";
|
||||
|
||||
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
||||
mimeBodyPart.setContent(msg, "text/html");
|
||||
|
||||
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
|
||||
attachmentBodyPart.attachFile(new File("pom.xml"));
|
||||
|
||||
Multipart multipart = new MimeMultipart();
|
||||
multipart.addBodyPart(mimeBodyPart);
|
||||
multipart.addBodyPart(attachmentBodyPart);
|
||||
|
||||
message.setContent(multipart);
|
||||
|
||||
Transport.send(message);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String ... args) {
|
||||
new EmailService("smtp.mailtrap.io", 25, "87ba3d9555fae8", "91cb4379af43ed");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.urlconnection;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class PostJSONWithHttpURLConnection {
|
||||
|
||||
public static void main (String []args) throws IOException{
|
||||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
|
||||
URL url = new URL ("https://reqres.in/api/users");
|
||||
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
|
||||
con.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
con.setRequestProperty("Accept", "application/json");
|
||||
|
||||
con.setDoOutput(true);
|
||||
|
||||
//JSON String need to be constructed for the specific resource.
|
||||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
|
||||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
|
||||
|
||||
try(OutputStream os = con.getOutputStream()){
|
||||
byte[] input = jsonInputString.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int code = con.getResponseCode();
|
||||
System.out.println(code);
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
System.out.println(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user