`

Java利用HttpURLConnection发送请求

    博客分类:
  • Java
阅读更多

1、post请求

String urlStr = "http://localhost:8080/user/sendMessage";
	String result = "";
	URL url = null;
	HttpURLConnection conn = null;
	OutputStream os = null;
	BufferedReader in = null;
	try {
		url = new URL(urlStr);
		conn= (HttpURLConnection) url.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		//设置连接超时
		conn.setConnectTimeout(50000);
		//设置读取超时
		conn.setReadTimeout(50000);	
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.connect();
		//参数
		String param = "name=xxx&token=xxx";
		//写入数据
		os = conn.getOutputStream();
		os.write(param.getBytes("UTF-8"));
		os.flush();
		os.close();
		//读取返回数据
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				result += inputLine;
			}
			in.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (conn != null) conn.disconnect();
	}

 2、get请求

String urlStr = "http://localhost:8080/user/sendMessage?&name=xxx&token=xxx";
	BufferedReader reader = null;
	String line = null;
	StringBuffer result = new StringBuffer();
	URL url;
	try {
		url = new URL(urlStr);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		con.setUseCaches(false);
		reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
		while (null != (line = reader.readLine())) {
			result.append(line);
		}
		if (reader != null) {
			reader.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}

 

 

 

 

 

分享到:
评论

相关推荐

    Java利用HttpURLConnection发送post请求上传文件.docx

    Java利用HttpURLConnection发送post请求上传文件

    java利用java.net.URLConnection发送HTTP请求的方法详解

    如何通过Java(模拟浏览器)发送HTTP请求是我们在日常经常会遇到的问题,下面这篇文章主要给大家介绍了关于java利用java.net.URLConnection发送HTTP请求的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,...

    搜索链接Java网络爬虫(蜘蛛)源码-zhizhu

    网页抓取:使用Java的网络编程库(如HttpURLConnection、Apache HttpClient等)来发送HTTP请求,获取网页的HTML内容。 网页解析:利用Java的HTML解析库(如Jsoup、HtmlCleaner等)来解析网页的HTML结构,提取出所需...

    疯狂Android讲义.part2

    19.3 发送请求的工具类 646 19.4 用户登录 647 19.4.1 处理登录的Servlet 648 19.4.2 用户登录 649 19.5 查看流拍物品 655 19.5.1 查看流拍物品的Servlet 655 19.5.2 查看流拍物品 656 19.6 管理物品种类 661 19.6.1...

    疯狂Android讲义.part1

    19.3 发送请求的工具类 646 19.4 用户登录 647 19.4.1 处理登录的Servlet 648 19.4.2 用户登录 649 19.5 查看流拍物品 655 19.5.1 查看流拍物品的Servlet 655 19.5.2 查看流拍物品 656 19.6 管理物品种类 661 19.6.1...

    精通ANDROID 3(中文版)1/2

    11.1.7 使用HttpURLConnection  11.1.8 使用AndroidHttpClient  11.1.9 使用后台线程(AsyncTask)  11.1.10 使用AsyncTask处理配置更改  11.1.11 使用DownloadManager获取文件  11.2 使用Android服务  ...

    精通Android 3 (中文版)2/2

    11.1.7 使用HttpURLConnection  11.1.8 使用AndroidHttpClient  11.1.9 使用后台线程(AsyncTask)  11.1.10 使用AsyncTask处理配置更改  11.1.11 使用DownloadManager获取文件  11.2 使用Android服务  ...

    疯狂Android讲义源码

     13.3.1 使用HttpURLConnection 496  13.3.2 使用Apache HttpClient 501  13.4 使用WebView视图  显示网页 505  13.4.1 使用WebView浏览网页 506  13.4.2 使用WebView加载HTML  代码 507  13.5 使用Web ...

    HttpClient以及获取页面内容应用

    3.调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。 client.executeMethod(method); 5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取...

Global site tag (gtag.js) - Google Analytics