先手寫http報文,分隔符和數(shù)據(jù)都必須每個都非常清楚

image.png
package 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;
//請求參數(shù)串,只有GET會有,請求地址的問號后面的串
public String queryString;
//協(xié)議版本:HTTP1.1
public String protocal;
//請求協(xié)議:http、https、ftp
public String scheme;
//主機地址:ip地址,或者域名,127.0.0.1
public String serverName;
//端口號:8080
public String port;
//應(yīng)用名: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類
- 封裝變量:存數(shù)據(jù)
- 封裝方法:執(zhí)行操作
http://blog.csdn.net/yun90/article/GetRelatedArticles?pageindex=2&articleId=23462041
| java方法 | 返回結(jié)果 | 含義 |
|---|---|---|
| request.getMethod() | GET | HTTP請求的的方法名,默認是GET,也可以指定PUT或POST |
| request.getRequestURL() | http://localhost:8082/TestReq/MyServlet | 客戶請求求的URL,不包括參數(shù)數(shù)據(jù) |
| request.getQueryString() | username=李雷&age=20 | 返回URL上的參數(shù)部分的字符串,必須是GET的請求才有效,不然報錯.這里的URL參數(shù)中帶有中文,是通過字符轉(zhuǎn)碼的:String eQuery=new String(request.getQueryString().getBytes("ISO-8859-1")) |
| request.getProtocol() | HTTP/1.1 | 返回請求的協(xié)議名和版本,如HTTP/1.1等 |
| request.getScheme() | http | 返回請求的方案名,如http,ftp,https等 |
| request.getServerName() | localhost | 服務(wù)器主機名 |
| request.getRemoteAddr() | 127.0.0.1 | 發(fā)送請求的客戶端主機的IP |
| request.getServerPort() | 8082 | 服務(wù)器上web應(yīng)用的訪問端口 |
| request.getRequestURI() | /TestReq/MyServlet | 將URL的域名和尾隨的參數(shù)截取掉,剩下的那部分就是 |
| URIrequest.getContextPath() | /TestReq | 即斜桿加工程名 |
| request.getServletPath() | /MyServlet | 工程之后到參數(shù)之前的這部分字符串 |
| request.getContentLength() | -1 | 請求體內(nèi)容的長度,只對POST和PUT類型的請求有效 |
| request.getContentType() | null | 請求體內(nèi)容類型 |
response的響應(yīng)內(nèi)容:response.setContentType("text/html;charset=gbk"),才可以正常顯示頁面中文