2、通过query属性添加了一个名为拍中name的参数,值为John。
3、这将在地址栏袭茄山上显纳巧示为/usersname=John。
给拆凳http post传参,参考以下二个实例:哪模//serverURL url地址
HttpPost httpPost = new HttpPost(serverURL)
//param 为参数
StringEntity entity = new StringEntity(param)
entity.setContentType("application/x-www-form-urlencoded")
httpPost.setEntity(entity)
HttpResponse httpResponse = httpClient.execute(httpPost)
还可以用map作为参数
List<NameValuePair>旅缓旅 formparams = new ArrayList<NameValuePair>()
if(param!=null){
Set set = param.keySet()
Iterator iterator = set.iterator()
while (iterator.hasNext()) {
Object key = iterator.next()
Object value = param.get(key)
formparams.add(new BasicNameValuePair(key.toString(), value.toString()))
}
}
HTTP请求方式有两种,一种是GET方式,请求参数会用“?”作为分隔符跟在请求资源后面;另一种是POST方式,请求参数放在了最后面的位置。服务器监听到浏览器的链接,首先将HTTP请求信息保存判清下来,再进行解析。
// 请求信息的输入流
private InputStream input
//获得HTTP请求信息,并解析出请求使用的是GET还是POST,然后调用相应的处理方法进行处理
public void parseInput(){
byte [] b = new byte[1024]
try {
input.read(b)
} catch (IOException e) {
LogInfo.error("读取请求信息出错"+e.getMessage())
return
}
String inputStr = new String(b)
String type = inputStr.substring(0,inputStr.indexOf(" "))
if("GET".equals(type)){
parseGetString(inputStr)
}
else{
parsePostString(inputStr)
}
}
//路径信息,http://localhost:8088/CCB?account=abc&pwd=123,其中/CCB表示pathInfo
private String pathInfo
//请求资源路径,pathInfo中最后一个斜杆后米啊的字符串,如/bank/CCB,郑纤其中/CCB表示urlPattern
private String urlPattern
//请求参数,在Get请求中第一个问号后面的字符串,如account=abc&pwd=123
private String queryStr
//解析GET请求
public void parseGetString(String getStr){
String allStr = getStr
String info = allStr.substring(allStr.indexOf("/"),allStr.indexOf(" HTTP"))
int end = info.indexOf("?")
if(end == -1){
pathInfo = info
}else{
pathInfo = info.substring(0,end)
queryStr = info.substring(end + 1)
}
urlPattern = pathInfo.substring(pathInfo.lastIndexOf("/"))
parseQueryInfo(queryStr)
}
//解析POST请求
public void parsePostString(String postStr){
String qStr = postStr.trim()
pathInfo = postStr.substring(postStr.indexOf("/"),postStr.indexOf(" HTTP"))
urlPattern = pathInfo.substring(pathInfo.lastIndexOf("/"))
queryStr = qStr.substring(qStr.lastIndexOf(System.getProperty("line.separator"))).trim()
parseQueryInfo(queryStr)
}
解析出请求资源路径和请求参数就可以找到对应的资源发送给浏览喊冲仿器或根据请求参数做相应的处理,再将资源发送回去。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)