java http get post

package com.szseq.restwebservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtil
{
public String httpget(String url, String Authorization, String LogPath)
{
Log log = new Log();
URLConnection connection = null;
String aa = null;
try {
connection = new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "Fidder");
connection.setRequestProperty("Authorization", Authorization);
connection.setConnectTimeout(5000);
connection.connect();
InputStream fin = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(fin, "utf-8"));
StringBuffer buffer = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
}
aa = buffer.toString();
} catch (IOException e) {
e.printStackTrace();
log.saveLog(e, LogPath);
}
return aa;
}

public String httppost(String url, String Authorization, String param, String LogPath) {
Log log = new Log();
OutputStreamWriter out = null;
URLConnection connection = null;
String aa = null;
try {
connection = new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "Fidder");
connection.setRequestProperty("Authorization", Authorization);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
connection.setDoInput(true);
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
out.write(param);
out.flush();
out.close();
InputStream fin = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(fin, "utf-8"));
StringBuffer buffer = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
}
aa = buffer.toString();
} catch (IOException e) {
e.printStackTrace();
log.saveLog(e, LogPath);
}
return aa;
}
}

package com.szseq.restwebservice;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log
{
private File file = null;

public File getFile() {
return this.file;
}

public void setFile(File file) {
this.file = file;
}

public void saveLog(Exception e, String path) {
try {
this.file = new File(path);
PrintWriter writer = null;
FileWriter fileWrite = new FileWriter(this.file, true);
writer = new PrintWriter(fileWrite);
writer.append(System.getProperty("line.separator") +
new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
writer.append(System.getProperty("line.separator"));
writer.append(" *************************" + e.toString() + "*************************");
writer.append(System.getProperty("line.separator"));
e.printStackTrace(writer);
writer.flush();
writer.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

猜你喜欢

转载自www.cnblogs.com/feng666666/p/11081375.html
今日推荐