c httpurlconnectionn发送ack数据包是在什么情况下的

HttpURLConnection在底层是否复用socket的简单验证方式 | 写点什么下次自动登录
现在的位置:
& 综合 & 正文
Android网络编程原理及HttpURLConnection 用法
网络通信层次图
TCP报文段格式
建链过程中的“三次握手”
1、SYN(Synchronize Sequence Numbers)是TCP是传输控制协议中的一个标志位。如果该位被置为1,则表示这个报文是一个请求建立连接的报文。
2、ACK(ACKnowledge Character)中文:确认字符。也是TCP是传输控制协议的一个标志位。在数据通信传输中,接收站发给发送站的一种传输控制字符。它表示确认发来的数据已经接受无误。是一个用于确认的报文。
关闭链路的四次握手
1、FIN(finish) 结束,没有更多的数据发送
Http请求协议
GET /img/iknow/msg/msg.gif HTTP/1.1
Accept: */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.5.; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET4.0C)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
HTTP/1.1 200 OK
Content-Type: image/gif
Accept-Ranges: bytes
Last-Modified: Mon, 19 Dec :44 GMT
Expires: Wed, 13 Jul :55 GMT
Cache-Control: max-age=
Content-Length: 1141
Date: Mon, 03 Sep :55 GMT
Server: BWS/1.0
-----------------------------------------------------------------
HTTP/1.1 状态码 错误信息
Content-Type:
Content-length:(包体长度)
HttpURLConnection Get请求
* 1、android模拟器(simulator)把它自己作为了localhost,也就是说,代码中使用localhost或者127.0.0.1来访问,
都是访问模拟器自己!
* 2、如果想在模拟器simulator上面访问你的电脑,那么就使用android内置的IP 10.0.2.2 ,
10.0.2.2 是模拟器设置的特定ip,是你的电脑的别名alias
* 3、记住,在模拟器上用10.0.2.2访问你的电脑本机
private final String baseUrl = "http://10.0.2.2:8080/WebProj/main.jsp";
//HttpURLConnection Get请求
btnHttpURLConnection_Get.setOnClickListener(new OnClickListener()
public void onClick(View v)
InputStream is = null;
HttpURLConnection httpUrlConn = null;
String strAccountNumber = etAccountNumber.getText().toString();
String strPwd = etPwd.getText().toString();
StringBuffer sbUrl = new StringBuffer();
//http://10.0.2.2:8080/WebProj/main.jsp?textAccountNumber=abc&textPwd=123
.append(baseUrl)
.append("?textAccountNumber=")
//注意如果参数是中文,要做以下处理
.append(URLEncoder.encode(strAccountNumber, "utf-8"))
.append("&textPwd=")
.append(strPwd);
URL url = new URL(sbUrl.toString());
//打开连接
httpUrlConn = (HttpURLConnection) url.openConnection();
//获取连接状态
int rc = httpUrlConn.getResponseCode();
//连接成功
if(rc == HttpURLConnection.HTTP_OK)
//获取数据,先得到输入流
is = httpUrlConn.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is,"gbk"));
String inputLine = null;
StringBuffer sbContent = new StringBuffer();
while ((inputLine = buffer.readLine()) != null)
sbContent.append(inputLine + "\n");
//获取到的内容转换成 GBK 编码,防止中文乱码
//String strContent = new String(sbContent.toString().getBytes("iso-8859-1"), "gbk");
tvContent.setText(sbContent.toString());
buffer.close();
buffer = null;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
if(is != null)
is.close();
is = null;
catch (IOException e)
e.printStackTrace();
if(httpUrlConn != null)
//关闭连接
httpUrlConn.disconnect();
httpUrlConn = null;
HttpURLConnection Post请求
* 1、android模拟器(simulator)把它自己作为了localhost,也就是说,代码中使用localhost或者127.0.0.1来访问,
都是访问模拟器自己!
* 2、如果想在模拟器simulator上面访问你的电脑,那么就使用android内置的IP 10.0.2.2 ,
10.0.2.2 是模拟器设置的特定ip,是你的电脑的别名alias
* 3、记住,在模拟器上用10.0.2.2访问你的电脑本机
private final String baseUrl = "http://10.0.2.2:8080/WebProj/main.jsp";
//HttpURLConnection Post请求
btnHttpURLConnection_Post.setOnClickListener(new OnClickListener()
public void onClick(View v)
DataOutputStream dos = null;
InputStream is = null;
HttpURLConnection httpUrlConn = null;
String strAccountNumber = etAccountNumber.getText().toString();
String strPwd = etPwd.getText().toString();
URL url = new URL(baseUrl);
//获取一个连接
httpUrlConn = (HttpURLConnection) url.openConnection();
//允许连接输出
httpUrlConn.setDoOutput(true);
//允许连接输入
httpUrlConn.setDoInput(true);
//设置以 POST 方式发送请求
httpUrlConn.setRequestMethod("POST");
//设置 POST 请求不能使用缓存
httpUrlConn.setUseCaches(false);
//设置这个连接是否可以重定向
httpUrlConn.setInstanceFollowRedirects(true);
//设置请求头的域的值
//配置本连接的 CONTENT_TYPE 为 application/x-www-form-urlencoded
httpUrlConn.setRequestProperty(HTTP.CONTENT_TYPE, URLEncodedUtils.CONTENT_TYPE);
//连接,所有的配置信息要 connect() 方法前完成,httpUrlConn.getOutputStream 会隐含地进行 connect
httpUrlConn.connect();
//构造请求包体的内容
StringBuffer sbParam = new StringBuffer();
//textAccountNumber=abc&textPwd=123
.append("textAccountNumber=")
.append(URLEncoder.encode(strAccountNumber, "gbk"))
.append("&textPwd=")
.append(URLEncoder.encode(strPwd, "gbk"));
//获取连接的输出流
dos = new DataOutputStream(httpUrlConn.getOutputStream());
//将参数写入输出流
dos.writeBytes(sbParam.toString());
dos.flush();
dos.close();
dos = null;
//获取连接状态,当执行 getInputStream() 时,才将输出流中的数据发送出去
int rc = httpUrlConn.getResponseCode();//HttpURLConnection.HTTP_OK;
System.out.println("httpUrlConn.getResponseCode()=" + rc);
//连接成功
if(rc == HttpURLConnection.HTTP_OK)
//获取数据
is = httpUrlConn.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is,"gbk"));
String inputLine = null;
StringBuffer sbContent = new StringBuffer();
while ((inputLine = buffer.readLine()) != null)
sbContent.append(inputLine + "\n");
tvContent.setText(sbContent.toString());
buffer.close();
buffer = null;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
if(dos != null)
dos.close();
dos = null;
catch (IOException e)
e.printStackTrace();
if(is != null)
is.close();
is = null;
catch (IOException e)
e.printStackTrace();
if(httpUrlConn != null)
//关闭连接
httpUrlConn.disconnect();
httpUrlConn = null;
&&&&推荐文章:
【上篇】【下篇】使用HttpURLConnection发送Soap请求 -
- ITeye博客
博客分类:
说到webservice无非就是一个http请求。至于请求的方式,那就多种多样了。但无论如何,webserivce都是遵循一定的规范的这个规范就是WSDL,只要能看明白wsdl的描述,那么只要编写一个符合它规范的请求就能简单的调用webservice,以下是通过HttpURLConnection模拟Soap请求来调用一个webservice并获取返回的结果,至于怎么解析就看个人爱好了。
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.net.HttpURLC
import java.net.MalformedURLE
import java.net.URL;
public class Sender {
* 以下代码演示了如何使用HttpURLConnection发送Soap请求调用webservice,并获取结果
* @param args
* @throws MalformedURLException
* @throws IOException
public static void main(String[] args) throws MalformedURLException, IOException {
HttpURLConnection connection=(HttpURLConnection)new URL("http://localhost:8080/XFireService/services/BookService").openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","text/charset=UTF-8");
connection.setRequestProperty("soapaction","");
connection.connect();//连接服务器
OutputStream os=connection.getOutputStream();
String xml="&soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" &" +
&soapenv:Header/&" +
&soapenv:Body&" +
&putNames&" +
&!--Zero or more repetitions:--&" +
&string&xxx&/string&" +
&string&yyy&/string&" +
&/putNames&" +
&/soapenv:Body&" +
" &/soapenv:Envelope&";
os.write(xml.getBytes());
os.flush();
os.close();
InputStream is = connection.getInputStream();//获取数据,真正的请求从此处开始
byte[] bts=new byte[is.available()];
is.read(bts);
System.out.println(new String(bts));
atlantisholic
浏览: 3092 次
来自: 苏州
你好,你有没有服务端的代码啊!
我找了好多都是客户端的,但是 ...

我要回帖

更多关于 urlhttpconnection 的文章

 

随机推荐