หน้าเว็บ

วันอังคารที่ 13 ธันวาคม พ.ศ. 2554

Receive Response from HttpURLConnection

    
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

 class ConnectionUtils {

  public static String receiveResponse(HttpURLConnection conn)
      throws IOException {
    conn.setConnectTimeout(10000);
    conn.setReadTimeout(10000);
    // retrieve the response from server
    InputStream is = null;
    try {
      is = conn.getInputStream();
      int ch;
      StringBuffer sb = new StringBuffer();
      while ((ch = is.read()) != -1) {
        sb.append((charch);
      }
      return sb.toString();
    catch (IOException e) {
      throw e;
    finally {
      if (is != null) {
        is.close();
      }
    }
  }

}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น