接口对接的一系列问题
查找的资料
若依的java对post模拟请求
发送 POST 请求出现异常!
读取.properties文件问题
java模拟前端post请求获取数据,一开始就报错,链接param含有-,在输出中为?。但是在Java中直接写链接就没有这个问题,那就是读取.properties文件ProPertiesUtil 的问题,把字节流改为了字符流,在通过ProPertiesUtil 方法获取链接就正常了。
public class ProPertiesUtil {
private static ProPertiesUtil configManager;
private static Properties properties;
//通过私有构造方法读取配置文件里的信息
private ProPertiesUtil(String path){
//String path="test.properties";
//读取数据
InputStream is= ProPertiesUtil.class.getClassLoader().getResourceAsStream(path);
//通过Properties获取
properties=new Properties();
try {
// 原来的字节流
// properties.load(is);
// 现在的字符流
//这样输入的时候就可以不是一个一个字节读,而是一个一个字符读,再加上是个Buffer,效率会高很多。
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
properties.load(bf);
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sendPost文件问题
但是还是报错,同事让我用诺依的若依的java对post模拟请求
用了之后ioc报错400。
又查询到发送 POST 请求出现异常!
的第二条中文参数乱码,试了试可以用,但是返回的是乱码。于是认真的对比了这两个文件。请求和恢复都有中文,所以都得转码UTF-8。
遇到问题要静下心来,慢慢看。基础很重要,要不然一个转码问题卡住你。
public static String sendPost3(String url, String param)
{
// PrintWriter out = null;
OutputStreamWriter out = null;
// 因为字节-导致链接有错误,所以我改了编码为字符,现在再改回字节
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
String urlNameString = url;
log.info("sendPost - {}", urlNameString);
URL realUrl = new URL(urlNameString);
// URLConnection conn = realUrl.openConnection();
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
// conn.setRequestProperty("contentType", "utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);
// out = new PrintWriter(conn.getOutputStream());
// out.print(param);
// out = new OutputStreamWriter(conn.getOutputStream());
out = new OutputStreamWriter(conn.getOutputStream(),"utf-8");
// 问题是因为HttpURLConnection接收数据的时候 字符集默认的是GBK 要转码UTF-8
out.write(param);
System.out.println("param------------------------"+param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
// 接受有中文还是需要字节码的UTF-8的
// in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
log.info("recv - {}", result);
}
catch (ConnectException e)
{
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
}
catch (SocketTimeoutException e)
{
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
}
catch (IOException e)
{
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
}
catch (Exception e)
{
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
加油打工人!