JAVA HTTP 请求
来源:互联网 发布:a 算法八数码 编辑:程序博客网 时间:2024/06/10 16:53
JAVA HTTP 请求
标签(空格分隔): develop-java
java 服务器请求http请求,详情见下
try { System.out.println("sendKFMsg - openid = " + openId); JSONObject obj = new JSONObject(); obj.put("touser", openId); // 创建url资源 URL url = new URL("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + at); // 建立http连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置允许输出 conn.setDoOutput(true); conn.setDoInput(true); // 设置不用缓存 conn.setUseCaches(false); // 设置传递方式 conn.setRequestMethod("POST"); // 设置维持长连接 conn.setRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集: conn.setRequestProperty("Charset", "UTF-8"); //转换为字节数组 byte[] data = (obj.toString()).getBytes("UTF-8"); // 设置文件长度 conn.setRequestProperty("Content-Length", String.valueOf(data.length)); // 设置文件类型: conn.setRequestProperty("Content-type", "application/json;charset=UTF-8"); // 开始连接请求 conn.connect(); OutputStream out = conn.getOutputStream(); // 写入请求的字符串 out.write((obj.toString()).getBytes("UTF-8")); out.flush(); System.out.println(conn.getResponseCode()); // 请求返回的状态 if (conn.getResponseCode() == 200) { System.out.println("连接成功"); // 请求返回的数据 InputStream in = conn.getInputStream(); String a = null; try { byte[] data1 = new byte[in.available()]; in.read(data1); // 转成字符串 a = new String(data1,"UTF-8"); System.out.println(a); } catch (Exception e1) { e1.printStackTrace(); } } else { System.out.println("no++"); }} catch (Exception e) { e.printStackTrace();} finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); }}
请求 SOAP WebService
public String requestWebService(String uri, String namespace, String functionName, Map<String, Object> map) { String retString = ""; OutputStream out = null; BufferedReader in = null; try { String cont = ""; for (String key : map.keySet()) { if (map.get(key).getClass().getName().equals("java.util.ArrayList")) { String l = ""; List<String> list = (List<String>) map.get(key); for (String single : list) { l += "<tem:string>" + single + "</tem:string>"; } cont += "<tem:" + key + ">" + l + "</tem:" + key + ">"; } else if (map.get(key).getClass().getName().equals("java.lang.String")) { cont += "<tem:" + key + ">" + map.get(key) + "</tem:" + key + ">"; } } System.out.println(cont); // 传参整理 String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"" + namespace + "\">" + "<soapenv:Header/>" + "<soapenv:Body>" + "<tem:" + functionName + ">" + cont + "</tem:" + functionName + ">" + "</soapenv:Body>" + "</soapenv:Envelope>" ; System.out.println(content); // 创建url资源 URL url = new URL(uri); // 建立http连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置允许输出 conn.setDoOutput(true); conn.setDoInput(true); // 设置不用缓存 conn.setUseCaches(false); // 设置传递方式 conn.setRequestMethod("POST"); // 设置维持长连接 conn.setRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集: conn.setRequestProperty("Charset", "UTF-8"); //转换为字节数组 byte[] data = (content.toString()).getBytes("UTF-8"); // 设置文件长度 conn.setRequestProperty("Content-Length", String.valueOf(data.length)); // 设置文件类型: conn.setRequestProperty("Content-type", "text/xml;charset=UTF-8"); // 设置 SOAPAction conn.setRequestProperty("SOAPAction", "\"" + namespace + functionName + "\""); // 设置其他头 conn.setRequestProperty("Accept-Encoding", "gzip,deflate"); // 开始连接请求 conn.connect(); out = conn.getOutputStream(); // 写入请求的字符串 out.write((content.toString()).getBytes("UTF-8")); out.flush(); System.out.println(conn.getResponseCode()); // 请求返回的状态 if (conn.getResponseCode() == 200) { System.out.println("http connection successed !"); try { // 请求返回的数据 in = new BufferedReader(new InputStreamReader(conn.getInputStream()), "UTF-8"); String line; while ((line = in.readLine()) != null) { retString += line; } System.out.println(retString); // 解析xml dom4j //Document doc = null; //doc = DocumentHelper.parseText(retString); // 将字符串转为 XML //Element root = doc.getRootElement(); //Element returnElement = (Element)root.selectSingleNode("//SendResult"); //String returnString = returnElement.getText(); //System.out.println(returnString); //retString = returnString; } catch (Exception e1) { e1.printStackTrace(); } } else { System.out.println("http connection failed !"); } } catch (Exception e) { System.out.println("error : " + e.getMessage()); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } }}
0 0
- java 发送http请求
- Java HTTP请求
- java 的http请求
- Java异步HTTP请求
- java实现Http请求
- java 发起HTTP请求
- JAVA发送HTTP请求
- java 请求 http
- Java发送Http请求
- java 发http请求
- java 发送http请求
- Java发送http请求
- java发送http请求
- java发送http请求
- java发送http请求
- java解析http请求
- java发送http请求
- Java发起Http请求
- ubuntu增加新用户
- JAVA excel 导入导出
- ubuntu14.04桌面允许root用户登录设置
- Fragment之间传递数据
- Princepled Shader
- JAVA HTTP 请求
- pod install或update 提示 [!] Unable to find a specification for xxxx
- 使用Jmeter模拟浏览器
- 单元测试
- 修改过的webservice程序,发布后测试没有达到效果
- Android M Permission 运行时权限 学习笔记
- Linux系统基础之五:简单的系统文件管理
- eclipse中maven项目出现JavaBuilder handling CoreException错误
- TFT和IPS屏的区别