当前位置: 移动技术网 > IT编程>开发语言>Java > HttpClient Post 二进制/字节流/byte[]实例代码

HttpClient Post 二进制/字节流/byte[]实例代码

2017年12月08日  | 移动技术网IT编程  | 我要评论

 httpclient post 二进制/字节流/byte[]实例代码

httpclient 3.x

public class httphelper { 
  string m_url; 
  httpclient m_httpclient; 
 
  public httphelper(string url) { 
    m_url = url; 
    m_httpclient = new httpclient(); 
  } 
 
  public byte[] post(byte[] bytes, string contenttype) throws ioexception { 
    postmethod method = new postmethod(m_url); 
 
    if ((contenttype != null) && (contenttype.length() > 0)) 
      method.setrequestheader("content-type" , contenttype); 
    method.setrequestentity(new bytearrayrequestentity(bytes)); 
    int httpcode = m_httpclient.executemethod(method); 
    if (httpcode != httpstatus.sc_ok) 
      throw new ioexception("invalid httpstatus: " + httpcode); 
    inputstream respstream = method.getresponsebodyasstream(); 
    int respbodysize = respstream.available(); 
    if (respbodysize <= 0) 
      throw new ioexception("invalid respbodysize: " + respbodysize); 
    byte[] respbuffer = new byte[respbodysize]; 
    if (respstream.read(respbuffer) != respbodysize) 
      throw new ioexception("read respbody error"); 
    return respbuffer; 
  } 
 
  public string postxml(string str) throws ioexception { 
    byte[] reqbuffer = str.getbytes(charset.forname("utf-8")); 
    byte[] respbuffer = post(reqbuffer, "application/xml; charset=utf-8"); 
    string resp = new string(respbuffer, charset.forname("utf-8")); 
    return resp; 
  } 
} 

httpclient 4.x

public class httphelper { 
  closeablehttpclient m_httpclient; 
 
  public httphelper() { 
    m_httpclient = httpclients.createdefault(); 
  } 
 
  // send bytes and recv bytes 
  public byte[] post(string url, byte[] bytes, string contenttype) throws ioexception { 
    httppost httppost = new httppost(url); 
    httppost.setentity(new bytearrayentity(bytes)); 
    if (contenttype != null) 
      httppost.setheader("content-type", contenttype); 
    closeablehttpresponse httpresponse = m_httpclient.execute(httppost); 
    try { 
      httpentity entityresponse = httpresponse.getentity(); 
      int contentlength = (int) entityresponse.getcontentlength(); 
      if (contentlength <= 0) 
        throw new ioexception("no response"); 
      byte[] respbuffer = new byte[contentlength]; 
      if (entityresponse.getcontent().read(respbuffer) != respbuffer.length) 
        throw new ioexception("read response buffer error"); 
      return respbuffer; 
    } finally { 
      httpresponse.close(); 
    } 
  } 
 
  public byte[] post(string url, byte[] bytes) throws ioexception { 
    return post(url, bytes, null); 
  } 
 
  public string postxml(string url, string str) throws ioexception { 
    byte[] reqbuffer = str.getbytes(charset.forname("utf-8")); 
    byte[] respbuffer = post(url, reqbuffer, "application/xml; charset=utf-8"); 
    string resp = new string(respbuffer, charset.forname("utf-8")); 
    return resp; 
  } 
} 

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网