java http请求是什么意思(java的TCP和HTTP有什么区别)
各位老铁们好,相信很多人对java http请求是什么意思都不是特别的了解,因此呢,今天就来为大家分享下关于java http请求是什么意思以及java的TCP和HTTP有什么区别的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!
java HttpPost怎么传递参数
public class HttpURLConnectionPost{
/**
*@param args
*@throws IOException
*/
public static void main(String[] args) throws IOException{
readContentFromPost();
}
public static void readContentFromPost() throws IOException{
// Post请求的url,与get不同的是不需要带参数
URL postUrl= new URL("http://www.xxxxxxx.com");
//打开连接
HttpURLConnection connection=(HttpURLConnection) postUrl.openConnection();
//设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
//默认是 GET方式
connection.setRequestMethod("POST");
// Post请求不能使用缓存
connection.setUseCaches(false);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(true);
//配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
//意思是正文是urlencoded编码过的form参数
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
//要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
DataOutputStream out= new DataOutputStream(connection
.getOutputStream());
//正文,正文内容其实跟get的URL中'?'后的参数字符串一致
String content="字段名="+ URLEncoder.encode("字符串值","编码");
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(content);
//流用完记得关
out.flush();
out.close();
//获取响应
BufferedReader reader= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line= reader.readLine())!= null){
System.out.println(line);
}
reader.close();
//该干的都干完了,记得把连接断了
connection.disconnect();
}
扩展资料:关于Java HttpURLConnection使用
public static String sendPostValidate(String serviceUrl, String postData, String userName, String password){
PrintWriter out= null;
BufferedReader in= null;
String result="";
try{
log.info("POST接口地址:"+serviceUrl);
URL realUrl= new URL(serviceUrl);
//打开和URL之间的连接
URLConnection conn= realUrl.openConnection();
HttpURLConnection httpUrlConnection=(HttpURLConnection) conn;
//设置通用的请求属性
httpUrlConnection.setRequestProperty("accept","*/*");
httpUrlConnection.setRequestProperty("connection","Keep-Alive");
httpUrlConnection.setRequestProperty("user-agent","Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;SV1)");
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
Base64 base64= new Base64();
String encoded= base64.encodeToString(new String(userName+":"+password).getBytes());
httpUrlConnection.setRequestProperty("Authorization","Basic"+encoded);
//发送POST请求必须设置如下两行
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
//获取URLConnection对象对应的输出流
out= new PrintWriter(new OutputStreamWriter(httpUrlConnection.getOutputStream(),"utf-8"));
//发送请求参数
out.print(postData);
out.flush();
//定义BufferedReader输入流来读取URL的响应
in= new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(),"utf-8"));
String line;
while((line= in.readLine())!= null){
result+= line;
}
//
// if(!"".equals(result)){
// BASE64Decoder decoder= new BASE64Decoder();
// try{
// byte[] b= decoder.decodeBuffer(result);
// result= new String(b,"utf-8");
//} catch(Exception e){
// e.printStackTrace();
//}
//}
return result;
} catch(Exception e){
log.info("调用异常",e);
throw new RuntimeException(e);
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException e){
log.info("关闭流异常",e);
}
}
}
}
HttpServletResponse是什么意思
1.HttpServletResponse对象
(1).Web服务器收到一个http请求,会针对每个请求创建一个HttpServletRequest和HttpServletResponse对象,向客户端发送数据找HttpServletResponse,从客户端取数据找HttpServletRequest;
(2).HttpServletResponse对象可以向客户端发送三种类型的数据:a.响应头b.状态码c.数据
(3).自己去看HttpServletResponse的API
(4).rsponse常见应用:
a.使用OutputStream向客户端写入中文:
String data="中国";
OutputStream stream= response.getOutputStream();//获取一个向Response对象写入数据的流,当tomcat服务器进行响应的时候,会将Response中的数据写给浏览器
stream.write(data.getBytes("UTF-8"));
//此时在html页面会出现乱码,这是因为:服务器将"中国"按照UTF-8码表进行编码,得到对应的码值假设是98,99,服务器将码值发送给浏览器。浏览器默认按照GB2312进行解码,在GB2312码表中对应的字符已不是"中国"
正确代码如下:
response.setHeader("Content-type","text/html;charset=UTF-8");//向浏览器发送一个响应头,设置浏览器的解码方式为UTF-8
String data="中国";
OutputStream stream= response.getOutputStream();
stream.write(data.getBytes("UTF-8"));
b.使用Writer向客户端写入中文:
PrintWriter writer= response.getWriter();
writer.write("中国");//同样会出现乱码,这是因为我们将"中国"写入response对象时,tomcat服务器为了将数据通过网络传输给浏览器,必须进行编码,由于没有指定编码方式,默认采用ISO8859-1,
当浏览器接收到数据后,根据GBK解码必然出现乱码
正确代码如下:
response.setCharacterEncoding("UTF_8");//设置Response的编码方式为UTF-8
response.setHeader("Content-type","text/html;charset=UTF-8");//向浏览器发送一个响应头,设置浏览器的解码方式为UTF-8,其实设置了本句,也默认设置了Response的编码方式为UTF-8,但是开发中最好两句结合起来使用
//response.setContentType("text/html;charset=UTF-8");同上句代码作用一样
PrintWriter writer= response.getWriter();
writer.write("中国");
c.使用Response实现文件下载:
String path= this.getServletContext.getRealPath(“/中国.jpg”);
String fileName= path.subString(path.lastIndexOf(“\\”));
Response.setHeader(“content-disposition”,”attachment;filename”+URLENcode r.encode(fileName,”UTF-8”));//设置响应头,告诉浏览器,该响应是下载响应,如果文件名包含中文,必须使用URL编码
……对文件进行读写
怎么用java写一个http接口
一个servlet接口就可以了啊:
HTTP Header请求实例
下面的实例使用 HttpServletRequest的getHeaderNames()方法读取 HTTP头信息。该方法返回一个枚举,包含与当前的 HTTP请求相关的头信息。
一旦我们有一个枚举,我们可以以标准方式循环枚举,使用hasMoreElements()方法来确定何时停止,使用nextElement()方法来获取每个参数的名称。
//导入必需的java库
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Enumeration;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@WebServlet("/DisplayHeader")
//扩展HttpServlet类
publicclassDisplayHeaderextendsHttpServlet{
//处理GET方法请求的方法
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException
{
//设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriterout=response.getWriter();
Stringtitle="HTTPHeader请求实例-菜鸟教程实例";
StringdocType=
"<!DOCTYPEhtml>\n";
out.println(docType+
"<html>\n"+
"<head><metacharset=\"utf-8\"><title>"+title+"</title></head>\n"+
"<bodybgcolor=\"#f0f0f0\">\n"+
"<h1align=\"center\">"+title+"</h1>\n"+
"<tablewidth=\"100%\"border=\"1\"align=\"center\">\n"+
"<trbgcolor=\"#949494\">\n"+
"<th>Header名称</th><th>Header值</th>\n"+
"</tr>\n");
EnumerationheaderNames=request.getHeaderNames();
while(headerNames.hasMoreElements()){
StringparamName=(String)headerNames.nextElement();
out.print("<tr><td>"+paramName+"</td>\n");
StringparamValue=request.getHeader(paramName);
out.println("<td>"+paramValue+"</td></tr>\n");
}
out.println("</table>\n</body></html>");
}
//处理POST方法请求的方法
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doGet(request,response);
}
}
java的TCP和HTTP有什么区别
TCP是传输层协议,定义数据传输和连接方式的规范。握手过程中传送的包里不包含数据,三次握手完毕后,客户端与服务器才正式开始传送数据。
HTTP超文本传送协议(Hypertext Transfer Protocol)是应用层协议,定义的是传输数据的内容的规范。
HTTP协议中的数据是利用TCP协议传输的,特点是客户端发送的每次请求都需要服务器回送响应,它是TCP协议族中的一种,默认使用 TCP 80端口。
好比网络是路,TCP是跑在路上的车,HTTP是车上的人。每个网站内容不一样,就像车上的每个人有不同的故事一样。
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!