public static String gethtmlPost(String cookie, String htmltype,
String httpurl, String version,String MethodType,String Param,Boolean changeline)
throws IOException {
URL urlx = new URL(httpurl);
HttpURLConnection uc = (HttpURLConnection) urlx.openConnection();
//建立输入流,向指向的URL传入参数
uc.setDoOutput(true); //需要输出
uc.setDoInput(true); //需要输入
uc.setUseCaches(false); //不允许缓存
uc.setRequestMethod(MethodType); //设置POST方式连接
if (cookie != null && !cookie.trim().equals("")){
uc.setRequestProperty("Cookie", cookie);
}
//设置请求属性
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uc.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36");
if(MethodType.equals("POST")){
DataOutputStream dos=new DataOutputStream(uc.getOutputStream());
dos.write(Param.getBytes("utf-8")); //否则中文乱码
dos.flush();
dos.close();
}
//获得响应状态
int resultCode=uc.getResponseCode();
StringBuffer sb=new StringBuffer();
if(HttpURLConnection.HTTP_OK==resultCode){
String readLine=new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(uc.getInputStream(),htmltype));
while((readLine=responseReader.readLine())!=null){
if (changeline!=null && changeline == false){
sb.append(readLine);
}else{
sb.append(readLine).append("\n");
}
}
responseReader.close();
}
return sb.toString();
}
HttpUrlConnection post请求
猜你喜欢
转载自blog.csdn.net/qq_14955245/article/details/84326188
今日推荐
周排行