先手写http报文,分隔符和数据都必须每个都非常清楚
image.pngpackage com.guoyasoft.beans;
public class HttpRequest {
/*
* 请求行
*/
//请求方法:post、get、connect
public String method;
//请求URL=http://127.0.0.1:8080/wuling/api/firstAPI
public String reuqestURL;
//请求参数串,只有GET会有,请求地址的问号后面的串
public String queryString;
//协议版本:HTTP1.1
public String protocal;
//请求协议:http、https、ftp
public String scheme;
//主机地址:ip地址,或者域名,127.0.0.1
public String serverName;
//端口号:8080
public String port;
//应用名:wuling
public String contextPath;
//资源地址:/api/firstAPI
public String servletPath;
//请求头:有很多项,这里只写了2项做示例
public String contentLength;
public String contentType;
//请求正文
public String content;
}
package com.guoyasoft.services;
import com.guoyasoft.beans.HttpRequest;
public class HttpTools {
public String generatePostReq(HttpRequest bean){
StringBuilder sb=new StringBuilder();
sb.append(bean.method);
sb.append(" ");
sb.append(bean.scheme);
sb.append("://");
sb.append(bean.serverName);
sb.append(":");
sb.append(bean.port);
sb.append("/");
sb.append(bean.contextPath);
sb.append("/");
sb.append(bean.servletPath);
sb.append(" ");
sb.append(bean.protocal);
sb.append("\r\n");
sb.append("content-type :");
sb.append(bean.contentType);
sb.append("\r\n");
sb.append("\r\n");
sb.append(bean.content);
return sb.toString();
}
}
package com.guoyasoft.actions;
import com.guoyasoft.beans.HttpRequest;
import com.guoyasoft.services.HttpTools;
public class Test {
public static void main(String[] args) {
HttpRequest request=new HttpRequest();
request.method="POST";
request.scheme="http";
request.serverName="120.132.0.133";
request.port="15021";
request.contextPath="APIServer";
request.servletPath="mayijinfu/API01";
request.protocal="HTTP1.1";
request.contentType="text/xml";
request.content="<head><version>0.0.1</version></head>";
HttpTools tool=new HttpTools();
String result=tool.generatePostReq(request);
System.out.println(result);
}
}
image.png
image.png
image.png
image.png
java类
- 封装变量:存数据
- 封装方法:执行操作
http://blog.csdn.net/yun90/article/GetRelatedArticles?pageindex=2&articleId=23462041
java方法 | 返回结果 | 含义 |
---|---|---|
request.getMethod() | GET | HTTP请求的的方法名,默认是GET,也可以指定PUT或POST |
request.getRequestURL() | http://localhost:8082/TestReq/MyServlet | 客户请求求的URL,不包括参数数据 |
request.getQueryString() | username=李雷&age=20 | 返回URL上的参数部分的字符串,必须是GET的请求才有效,不然报错.这里的URL参数中带有中文,是通过字符转码的:String eQuery=new String(request.getQueryString().getBytes("ISO-8859-1")) |
request.getProtocol() | HTTP/1.1 | 返回请求的协议名和版本,如HTTP/1.1等 |
request.getScheme() | http | 返回请求的方案名,如http,ftp,https等 |
request.getServerName() | localhost | 服务器主机名 |
request.getRemoteAddr() | 127.0.0.1 | 发送请求的客户端主机的IP |
request.getServerPort() | 8082 | 服务器上web应用的访问端口 |
request.getRequestURI() | /TestReq/MyServlet | 将URL的域名和尾随的参数截取掉,剩下的那部分就是 |
URIrequest.getContextPath() | /TestReq | 即斜杆加工程名 |
request.getServletPath() | /MyServlet | 工程之后到参数之前的这部分字符串 |
request.getContentLength() | -1 | 请求体内容的长度,只对POST和PUT类型的请求有效 |
request.getContentType() | null | 请求体内容类型 |
response的响应内容:response.setContentType("text/html;charset=gbk"),才可以正常显示页面中文
网友评论