在做接口对接到时候 本地电脑测试代码正常执行,但到了另外电脑 代码执行过程虽然不报错 但是conn.getInputStream始终为NULL,把接口地址url直接复制到浏览器地址上是可以的 比较急哪位大神告诉下解决方法 百度了很多都没用
public static String post(String url, String data, String D_METHOD) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("Content-Type", "Application/json;charset=utf-8");
conn.setRequestProperty("accept", "application/json");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64)");
conn.setRequestProperty("sun.net.http.retryPost", "false");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
OutputStreamWriter pu = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out = new PrintWriter(pu);
// 发送请求参数
out.print(data);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送请求异常");
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}