资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

post请求java代码,post java请求

如何使用java发送post请求

/**

弥勒网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联建站2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

* 向指定 URL 发送POST方法的请求

*

* @param url

* 发送请求的 URL

* @param param

* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。

* @return 所代表远程资源的响应结果

*/

public static String sendPost(String url, String param) {

PrintWriter out = null;

BufferedReader in = null;

String result = "";

try {

URL realUrl = new URL(url);

// 打开和URL之间的连接

URLConnection conn = realUrl.openConnection();

// 设置通用的请求属性

conn.setRequestProperty("accept", "*/*");

conn.setRequestProperty("connection", "Keep-Alive");

conn.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 发送POST请求必须设置如下两行

conn.setDoOutput(true);

conn.setDoInput(true);

// 获取URLConnection对象对应的输出流

out = new PrintWriter(conn.getOutputStream());

// 发送请求参数

out.print(param);

// flush输出流的缓冲

out.flush();

// 定义BufferedReader输入流来读取URL的响应

in = new BufferedReader(

new InputStreamReader(conn.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送 POST 请求出现异常!"+e);

e.printStackTrace();

}

//使用finally块来关闭输出流、输入流

finally{

try{

if(out!=null){

out.close();

}

if(in!=null){

in.close();

}

}

catch(IOException ex){

ex.printStackTrace();

}

}

return result;

}

HttpPost发送字符串到服务器,服务器接收代码并显示怎么写Java代码?

服务器端接收客户端的请求的话,需要在服务器端的java文件实现HttpServlet这个接口,并且在web.xml里配置一个客户端的请求拦截。

web.xml里的代码里添加

servlet

servlet-nametestServlet/servlet-name!--这个名字可以自己定--

servlet-classcom.sun.testServlet/servlet-class!--这里是你需要接收客户端请求的那个类以及包名,也就是下面拦截到的url会转发到的那个类--

/servlet

servlet-mapping

servlet-nametestServlet/servlet-name!--和上面的name需要一样--

url-pattern/*/url-pattern!--什么类型的客户端请求会被拦截,/* 就是全拦截了--

/servlet-mapping

然后再服务器端的类文件,要实现 HttpServlet这个接口。并把doGet()方法和doPost()方法重写。

这两种方法分别对应的是客户端的get请求和post请求的处理,你的是post请求的话,就在doPost()方法内,写你的业务。

然后再用下面两句话,设置你要返回客户端的数据。

//这是设置你要返回去的数据。value才是你的数据,key是标签。

request.setAttribute("key", "value");

//这是设置你要返回去test.jsp这张页面。

request.getRequestDispatcher("test.jsp").forward(request, response);

不知道你是不是这个意思,你可以再去看看相关servlet方面的知识,

关于客户端和服务器端大概也就是有个servlet作为请求的拦截

然后经过相关判断后,选择性的传到服务器的相应类里面。

再经过类里面的业务,把得到需要的数据回传到指定的页面上。

java 怎样响应post请求

Http请求类

package wzh.Http;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.net.URLConnection;

import java.util.List;

import java.util.Map;

public class HttpRequest {

/**

* 向指定URL发送GET方法的请求

*

* @param url

* 发送请求的URL

* @param param

* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。

* @return URL 所代表远程资源的响应结果

*/

public static String sendGet(String url, String param) {

String result = "";

BufferedReader in = null;

try {

String urlNameString = url + "?" + param;

URL realUrl = new URL(urlNameString);

// 打开和URL之间的连接

URLConnection connection = realUrl.openConnection();

// 设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 建立实际的连接

connection.connect();

// 获取所有响应头字段

MapString, ListString map = connection.getHeaderFields();

// 遍历所有的响应头字段

for (String key : map.keySet()) {

System.out.println(key + "---" + map.get(key));

}

// 定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(new InputStreamReader(

connection.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送GET请求出现异常!" + e);

e.printStackTrace();

}

// 使用finally块来关闭输入流

finally {

try {

if (in != null) {

in.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

return result;

}

/**

* 向指定 URL 发送POST方法的请求

*

* @param url

* 发送请求的 URL

* @param param

* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。

* @return 所代表远程资源的响应结果

*/

public static String sendPost(String url, String param) {

PrintWriter out = null;

BufferedReader in = null;

String result = "";

try {

URL realUrl = new URL(url);

// 打开和URL之间的连接

URLConnection conn = realUrl.openConnection();

// 设置通用的请求属性

conn.setRequestProperty("accept", "*/*");

conn.setRequestProperty("connection", "Keep-Alive");

conn.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 发送POST请求必须设置如下两行

conn.setDoOutput(true);

conn.setDoInput(true);

// 获取URLConnection对象对应的输出流

out = new PrintWriter(conn.getOutputStream());

// 发送请求参数

out.print(param);

// flush输出流的缓冲

out.flush();

// 定义BufferedReader输入流来读取URL的响应

in = new BufferedReader(

new InputStreamReader(conn.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送 POST 请求出现异常!"+e);

e.printStackTrace();

}

//使用finally块来关闭输出流、输入流

finally{

try{

if(out!=null){

out.close();

}

if(in!=null){

in.close();

}

}

catch(IOException ex){

ex.printStackTrace();

}

}

return result;

}

}

调用方法:

public static void main(String[] args) {

//发送 GET 请求

String s=HttpRequest.sendGet("", "key=123v=456");

System.out.println(s);

//发送 POST 请求

String sr=HttpRequest.sendPost("", "key=123v=456");

System.out.println(sr);

}

如何在java中发送post请求

package com.test;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

public class D {

public static void main(String[] args){

ListNameValuePair nvps= new ArrayListNameValuePair();

nvps.add(new BasicNameValuePair("id", "1"));

String url="";

HttpClient httpClient = null;

String response="";

try {

HttpPost post = new HttpPost(url);

post.setHeader("Connection", "close");

httpClient = new DefaultHttpClient();

post.setEntity(new UrlEncodedFormEntity(nvps));

HttpResponse httpres= httpClient.execute(post);

if (httpres.getStatusLine().getStatusCode() = 300) {

System.out.println("Request Failed,Code:" + httpres.getStatusLine().getStatusCode() + ",URL:" + url);

}

response = EntityUtils.toString(httpres.getEntity(), "utf-8");

}catch(Exception e){

e.printStackTrace();

}finally{

if(httpClient!=null){

httpClient.getConnectionManager().shutdown();

}

}

System.out.println(response);

}

}

需要httpclient-4.1.3.jar,httpcore-4.1.4.jar和commons-logging-1.1.1.jar

如何使用java模拟post请求

你要导入httpclient的jar包,要是你请求参数格式是json的或者返回的是json格式数据,你还需要导入json包

/**

* post请求

* @param url url地址

* @param jsonParam 参数

* @param noNeedResponse 不需要返回结果

* @return

*/

public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){

//post请求返回结果

DefaultHttpClient httpClient = new DefaultHttpClient();

JSONObject jsonResult = null;

HttpPost method = new HttpPost(url);

try {

if (null != jsonParam) {

//解决中文乱码问题

StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");

entity.setContentEncoding("UTF-8");

entity.setContentType("application/json");

method.setEntity(entity);

}

HttpResponse result = httpClient.execute(method);

url = URLDecoder.decode(url, "UTF-8");

/**请求发送成功,并得到响应**/

if (result.getStatusLine().getStatusCode() == 200) {

String str = "";

try {

/**读取服务器返回过来的json字符串数据**/

str = EntityUtils.toString(result.getEntity());

if (noNeedResponse) {

return null;

}

/**把json字符串转换成json对象**/

jsonResult = JSONObject.fromObject(str);

} catch (Exception e) {

logger.error("post请求提交失败:" + url, e);

}

}

} catch (IOException e) {

logger.error("post请求提交失败:" + url, e);

}

return jsonResult;

}


本文题目:post请求java代码,post java请求
本文路径:http://cdkjz.cn/article/hejohd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220