`
Luob.
  • 浏览: 1584316 次
  • 来自: 上海
社区版块
存档分类
最新评论

发送 GET 和 POST请求

URL 
阅读更多
/**
	 *@param url 发送请求的url
	 *@param param 请求参数 请求参数应该是 name=xx&pass=xxxx 
	 */
	public static String sendGet(String url,String param){
		String result="";
		BufferedReader bfr=null;
		try {
			String urlName=url+"?"+param;
			URL realUrl=new URL(urlName);
			URLConnection conn=realUrl.openConnection();
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-age", "Mozilla/4.0 (compatible;MSIE 6.0;Window NT 5.1;SV1)");
			
			//建立实际的连接
			conn.connect();
			Map<String,List<String>> map=conn.getHeaderFields();
			//遍历所有的响应头字段
			for (String key: map.keySet()) {
				System.out.println(key+"--->"+map.get(key));
			}
			//定义 BufferedRead输入流来读取URL的响应
			bfr=new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			String line;
			while((line=bfr.readLine())!=null){
				result+="\n"+line;
			}
		} catch (Exception e) {
			System.out.print("发送GET请求出现异常:"+e);
			e.printStackTrace();
		}finally{
			try {
				if(bfr!=null)
					bfr.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
		
	}
	
	/**
	 *@param url 发送请求的url
	 *@param param 请求参数 请求参数应该是 name=xx&pass=xxxx 
	 */
	public static String sendPost(String url,String param){
		PrintWriter out=null;
		BufferedReader in=null;
		String result="";
		try {
			URL realUrl=new URL(url);
			URLConnection conn=realUrl.openConnection();
			//设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-age", "Mozilla/4.0 (compatible;MSIE 6.0;Window NT 5.1;SV1)");
			
			//发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			out=new PrintWriter(conn.getOutputStream());
			out.print(param);
			out.flush();
			in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while((line=in.readLine())!=null){
				result+="\n"+line;
			}
		} catch (Exception e) {
			System.out.println("发送POST请求出现异常:"+e);
			e.printStackTrace();
		}finally{
			try {
				if(out!=null)
					out.close();
				if(in!=null)
					in.close();
				
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
	


Get 发送 数据
public String getJosnByUrl(String url) {
		StringBuffer josn = new StringBuffer();
		BufferedReader reader = null;
		InputStream inputStream = null;
		HttpURLConnection httpURLConnection = null;
		try {
			URL httpUrl = new URL(url);
			URLConnection connection = httpUrl.openConnection();
			httpURLConnection = (HttpURLConnection) connection;
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setReadTimeout(30000);// 无响应30秒,断开会话
			httpURLConnection.setConnectTimeout(30000);
			httpURLConnection.setRequestMethod("GET");
			httpURLConnection.setRequestProperty("Referer","http://taobao.51bi.com");
			httpURLConnection.setRequestProperty("Accept-Language","zh-cn");
			httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MALC)");
			httpURLConnection.connect();
			inputStream = httpURLConnection.getInputStream();// 发送连接
			reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				josn.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(reader != null)
					reader.close();
				if(inputStream != null)
					inputStream.close();
				if(httpURLConnection != null)
					httpURLConnection.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return josn.toString().trim();
	}




post 发送数据xml 或者json 然后读取返回值
public String getJosnByUrl(String url,String param) {
		StringBuffer josn = new StringBuffer();
		BufferedReader reader = null;
		PrintWriter out=null;
		InputStream inputStream = null;
		HttpURLConnection httpURLConnection = null;
		try {
			URL httpUrl = new URL(url);
			URLConnection connection = httpUrl.openConnection();
			httpURLConnection = (HttpURLConnection) connection;
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setReadTimeout(30000);// 无响应30秒,断开会话
			httpURLConnection.setConnectTimeout(30000);
			httpURLConnection.setRequestMethod("POST");
			
			//httpURLConnection.setRequestProperty("Referer","http://taobao.51bi.com");
			httpURLConnection.setRequestProperty("Accept-Language","zh-cn");
			httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MALC)");
			httpURLConnection.connect();
			out=new PrintWriter(httpURLConnection.getOutputStream());
			out.write(param);//数据写入缓存中
			out.flush();
			inputStream = httpURLConnection.getInputStream();// 发送连接
		
			reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				josn.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(out!=null)
					out.close();
				if(reader != null)
					reader.close();
				if(inputStream != null)
					inputStream.close();
				if(httpURLConnection != null)
					httpURLConnection.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return josn.toString().trim();
	}
分享到:
评论

相关推荐

    httpclient用法,发送get和post请求,设置header

    httpclient的用法,发送get请求和post请求,设置header

    使用java发送get和post请求实践

    在 Java 中发送 GET 和 POST 请求是非常常见的操作,今天我们将通过使用 Apache HttpClient 库来实现这些操作。 什么是 Apache HttpClient 库? Apache HttpClient 库是 Apache 软件基金会提供的一个开源库,用于...

    后台模拟发送GET和POST请求

    本文将深入探讨如何利用Java的HttpClient库在后台模拟发送GET和POST请求,以及如何处理中文乱码问题。 首先,我们来理解GET和POST两种请求方法。GET请求通常用于获取服务器上的资源,它将参数附加到URL中,具有可...

    HttpClient 发送get和post请求

    一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者

    HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件

    HttpClient是Java中用于执行HTTP请求的一个强大库,它提供了丰富的功能,可以方便地进行GET、POST请求,并且能够处理复杂的网络交互,包括发送文件等操作。下面我们将详细讨论HttpClientUtil工具类如何实现这些功能...

    C# 使用Get和Post请求获取数据

    在Web开发中,与服务器进行交互的一个关键方法是通过HTTP(超文本传输协议)发送GET和POST请求。这两个方法是HTTP协议中最基本的请求类型,用于从服务器获取或向服务器提交数据。 首先,让我们详细了解GET请求。GET...

    向web发送get和post请求的源码

    完整的向web发送get已经post请求的java程序

    http发送Get和Post请求工具类

    在`HttpUtils`中,通常会包含两个主要方法:`sendGetRequest`和`sendPostRequest`,分别用于执行GET和POST请求。 1. **GET请求**: - GET请求常用于获取服务器上的资源,它是无状态的,参数通过URL传递。`...

    java HttpClient 发送GET请求和带有表单参数的POST请求教程例子

    ### Java HttpClient 发送GET请求和带有表单参数的POST请求详解 #### 一、概述 在Java编程中,处理HTTP请求是一项常见的需求,特别是在与Web服务进行交互时。Apache HttpClient库提供了一种强大的方法来执行HTTP...

    AFNetwork发送GET/POST请求

    在这个教程中,我们将深入探讨如何使用AFNetworking发送GET和POST请求,并理解其工作原理。 首先,我们要了解GET和POST请求的基本概念。GET请求通常用于获取服务器上的数据,它是幂等的,即多次相同GET请求结果相同...

    c# http协议,实现get或post发送请求 并返回内容

    以下是如何使用HttpClient发送GET和POST请求的示例: **GET请求示例:** ```csharp using System.Net.Http; using System.Threading.Tasks; public async Task&lt;string&gt; SendHttpGetRequest(string url) { using ...

    java发送http/https请求(get/post)代码

    本文将详细讲解如何使用Java发送GET和POST请求,以及涉及的HTTPS安全连接。 首先,理解HTTP和HTTPS的区别至关重要。HTTP(超文本传输协议)是一种用于分发超媒体信息的应用层协议,而HTTPS(超文本传输安全协议)是...

    java发送get或post请求源码

    例如,使用Apache HttpClient库发送GET和POST请求会更加直观: 3. **使用Apache HttpClient库发送请求**: Apache HttpClient库提供了更强大的功能,例如支持多线程、超时控制、重试策略等。 - 发送GET请求: ...

    HttpClient(用CloseableHttpClient发送get/post请求)

    HttpClient(用CloseableHttpClient发送get/post请求)

    java发送http/https请求(get/post)Demo,亲测可用

    这里我们将深入探讨如何使用Java发送GET和POST请求,以及处理JSON数据。 首先,让我们关注GET请求。GET请求主要用于从服务器获取资源,其参数通常包含在URL中。在Java中,可以使用`HttpURLConnection`类或者第三方...

    html页面模式get/post请求

    对于POST请求,特别是当数据需要以JSON格式发送时,我们需要借助JavaScript,通常使用AJAX(Asynchronous JavaScript and XML)技术,通过XMLHttpRequest或Fetch API来实现。JSON是一种轻量级的数据交换格式,它的...

    MFC的HttpClient的Get和Post方法

    在Microsoft Foundation Class (MFC)库中,HttpClient是用于实现HTTP通信的重要工具,特别是对于发送GET和POST请求。本文将详细讲解如何在MFC中利用HttpClient进行GET和POST操作。 首先,我们需要理解HTTP协议的...

    Ajax中get和post区别

    在客户端代码中,我们使用XMLHttpRequest对象创建了一个Get请求和一个Post请求,并将参数传递给服务器端。在服务器端代码中,我们使用Request对象来获取参数,并返回结果。 结论 Get和Post请求都是HTTP请求方法,...

    java发送post和get请求源码及jar包

    这两个例子展示了如何使用Java内置的HttpURLConnection类发送GET和POST请求。然而,对于更复杂的场景,如管理cookies、重定向、超时控制等,使用Apache HttpClient或OkHttp等第三方库可能会更方便和强大。 在实际...

    android 后台 get和post请求数据

    总结,Android后台发送GET和POST请求主要涉及HTTP协议的使用、数据编码、网络请求库的选择以及异步处理。理解这些知识点对于开发能与服务器进行有效通信的Android应用至关重要。在处理过程中,要注意数据的安全性和...

Global site tag (gtag.js) - Google Analytics