崩壊である。
これに関係なく書いていたコードを書き直してた。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
public final class HttpUtilz{
public final static String KEY_CONTENT_TYPE = "Content-Type";
public final static String KEY_CONNECTION = "Connection";
public final static String POST = "POST";
public final static String GET = "GET";
private HttpUtilz(){}
public static HttpUtilzResponse send(URL url,Map<String,String> headerMap, String method
,String str,int timeout,String encode) throws Exception {
return send(url,headerMap,null,null,method,str,timeout,encode);
}
public static HttpUtilzResponse send(URL url,String str,int timeout) throws Exception{
Map<String,String> map = new HashMap<String,String>();
map.put(KEY_CONTENT_TYPE,"text/xml");
map.put(KEY_CONNECTION,"close");
return send(url,map,POST,str,timeout,"UTF-8");
}
public static HttpUtilzResponse send(URL url,String str) throws Exception{
return send(url,str,30000);
}
public static HttpUtilzResponse send(URL url,Map<String,String> headerMap,String user,String passwd
,String method,String str,int timeout,String encode) throws Exception{
HttpURLConnection uc = null;
OutputStreamWriter osw = null;
HttpUtilzResponse res = new HttpUtilzResponse();
try{
uc = (HttpURLConnection)url.openConnection();
uc.setDoOutput(true);
uc.setReadTimeout(timeout);
uc.setRequestMethod(method);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss",Locale.US);
uc.setRequestProperty("Date"," "+sdf.format(new Date())+" GMT");
if (method.equals(POST)){
uc.setRequestProperty("Content-Length",Integer.toString(str.getBytes(encode).length));
}
if (user != null && passwd != null){
uc.setRequestProperty("Authorization"," Basic "+Base64Utilz.base64Encode(user+":"+passwd));
}
for(Iterator<String> it=headerMap.keySet().iterator();it.hasNext();){
String key = it.next();
uc.setRequestProperty(key,headerMap.get(key));
}
uc.connect();
if (method.equals(POST)){
osw = new OutputStreamWriter(uc.getOutputStream(), encode);
osw.write(str);
osw.flush();
}
try{
try{
char buf = new char[1024];
int n;
while((n = in.read(buf)) >= 0){
out.append(buf, 0, n);
}
}finally{
in.close();
}
}catch(IOException e){
throw new RuntimeException(e);
}
res.setMessage(out.toString());
}catch(IOException e){
res.setResponseMessage(e.getMessage());
if (uc != null){
InputStream error_in = uc.getErrorStream();
if (error_in != null){
BufferedReader in_e = new BufferedReader(new InputStreamReader(error_in,encode));
StringBuffer out_e = new StringBuffer();
char buf = new char[1024];
int n;
try{
while*1 >= 0){
out_e.append(buf, 0, n);
}
}finally{
in_e.close();
}
res.setMessage(out_e.toString());
}
}
}catch(Exception e){
throw e;
}finally{
if (osw != null){ osw.close(); }
if (uc != null){ uc.disconnect(); }
}
return res;
}
public static HttpUtilzResponse send(URL url,String user,String passwd,String str,int timeout) throws Exception{
Map<String,String> map = new HashMap<String,String>();
map.put(KEY_CONTENT_TYPE,"text/xml");
map.put(KEY_CONNECTION,"close");
return send(url,map,user,passwd,str,POST,timeout,"UTF-8");
}
public static HttpUtilzResponse send(URL url,String user,String passwd,String str) throws Exception{
return send(url,user,passwd,str,30000);
}
public static HttpUtilzResponse send(URL url,String user,String passwd, String method, String str) throws Exception{
Map<String,String> map = new HashMap<String,String>();
map.put(KEY_CONTENT_TYPE, "text/plain");
map.put(KEY_CONNECTION, "close");
return send(url,map,user,passwd,str,method,30000,"UTF-8");
}
}
*1:n = in_e.read(buf