温馨提示×

java后台https如何写

九三
228
2021-02-18 10:49:25
栏目: 编程语言

java后台https如何写

在java后台书写https的方法

public static String httpRequest(String requestUrl,String requestMethod,String outputStr){

StringBuffer buffer=null;

try{

URL url=new URL(requestUrl);

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setRequestMethod(requestMethod);

conn.connect();

//往服务器端写内容 也就是发起http请求需要带的参数

if(null!=outputStr){

OutputStream os=conn.getOutputStream();

os.write(outputStr.getBytes("utf-8"));

os.close();

}

//读取服务器端返回的内容

InputStream is=conn.getInputStream();

InputStreamReader isr=new InputStreamReader(is,"utf-8");

BufferedReader br=new BufferedReader(isr);

buffer=new StringBuffer();

String line=null;

while((line=br.readLine())!=null){

buffer.append(line);

}

}catch(Exception e){

e.printStackTrace();

}

return buffer.toString();

}

0