Http 가 네트워크 에 접근 하 는 GET 와 POST
public class HttpGetUtil {
// get
public static InputStream getInputStream() {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL("http://www.baidu.com/img/bd_logo1.png");
if (url != null) {
httpURLConnection = (HttpURLConnection) url.openConnection();
//
httpURLConnection.setConnectTimeout(3000);
//
httpURLConnection.setDoInput(true);
// GET
httpURLConnection.setRequestMethod("GET");
//
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
inputStream = httpURLConnection.getInputStream();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
//
public void saveImageToDisk() {
InputStream inputStream = getInputStream();
byte[] data = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("D://test.png");
while ((len = inputStream.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class HttpPostUtil {
private static URL url;
// post
public static String sendPostMessage(Map<String, String> params, String encode) {
//
StringBuffer buffer = new StringBuffer();
buffer.append("?");
try {
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
//
buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
// &
buffer.deleteCharAt(buffer.length() - 1);
}
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);//
urlConnection.setDoOutput(true);//
//
byte[] mydata = buffer.toString().getBytes();
//
urlConnection.setRequestProperty("Content-type", "application/x-www-from-urlencoded");
urlConnection.setRequestProperty("Content-length", String.valueOf(mydata.length));
// ,
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(mydata, 0, mydata.length);
outputStream.close();
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
return changeInputStream(urlConnection.getInputStream(), encode);
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
//
private static String changeInputStream(InputStream inputStream, String encode) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) {
try {
while ((len = inputStream.read()) != -1) {
outputStream.write(data, 0, len);
}
result = new String(outputStream.toByteArray(), encode);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
빠른 팁: SingleStoreDB의 데이터 API 사용SingleStoreDB는 HTTP 연결을 통해 SQL 문을 실행하는 데 사용할 수 있는 을 제공합니다. 이 짧은 문서에서는 이 데이터 API를 사용하는 방법에 대한 예를 보여줍니다. A는 무료 SingleStore...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.