Closed ZKL2337 closed 3 years ago
huan
导入 java.io.ByteArrayOutputStream; 导入 java.io.IOException; 导入 java.io.InputStream; 导入 java.net.HttpURLConnection; 导入 java.net.URL;
公开课 apitest {
/** * Get请求,获得返回数据 * @param urlStr * @return */ private static String opUrl(String urlStr) { URL url = null; HttpURLConnection conn = null; InputStream is = null; ByteArrayOutputStream baos = null; try { url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5 * 10000); conn.setConnectTimeout(5 * 10000); conn.setRequestMethod("POST"); if (conn.getResponseCode() == 200) { is = conn.getInputStream(); baos = new ByteArrayOutputStream(); int len = -1; byte[] buf = new byte[128]; while ((len = is.read(buf)) != -1) { baos.write(buf, 0, len); } baos.flush(); String result = baos.toString(); return result; } else { throw new Exception("服务器连接错误!"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } try { if (baos != null) baos.close(); } catch (IOException e) { e.printStackTrace(); } conn.disconnect(); } return ""; } public static void main(String args []){ //msg参数就是传输过去的对话内容。 System.out.println(opUrl("http://www.weilaitec.com/cigirlrobot.cgr?key=UTNJK34THXK010T566ZI39VES50BLRBE8R66H5R3FOAO84J3BV&msg=你好&ip=119.25.36.48&userid=jackli&appid=52454214552")); }
}
0.004 BTC
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
public class apitest {
}