欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-21 19:53:46
 httpclient post 二进制/字节流/byte[]实例代码 httpclient 3.x public class httphelpe...

 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; 
  } 
} 

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