http服务向服务器发送并接受数据--------------HttpClient对象向服务器发送并接受数据

来源:互联网 发布:2017甘肃省网络研修 编辑:程序博客网 时间:2024/06/07 06:23

一、定义http服务向服务器发送并接受数据

(1)(连接工具类)

public class HttpURLConn { 

// 发送带参数的Post请求,得到返回的xml字符串

 public static String sendPost(String url, String param) throws Exception {
  String result = "";
  System.out.println("connent_center!");
  System.out.println("url="+url);
  try {
   URL httpurl = new URL(url);
   HttpURLConnection httpConn = (HttpURLConnection) httpurl
     .openConnection();
   httpConn.setDoOutput(true);
   httpConn.setDoInput(true);
//   httpConn.setRequestMethod("POST");
   PrintWriter out = new PrintWriter(httpConn.getOutputStream());
   out.print(URLEncoder.encode(param,"GBK"));
   out.print(param);
   out.flush();
   out.close();
   BufferedReader in = new BufferedReader(new InputStreamReader(
     httpConn.getInputStream()));
   String line;
   while ((line = in.readLine()) != null) {
    result += line;
   }
   in.close();
  } catch (Exception e) {
   // System.out.println("没有结果!" + e);
   log.error("sendPost fail", e);
   throw e;
  }
  log.debug(result);
  return result;
 }

 

(2)调用上边方法的测试类

public class Test{

    public static void main(String[] args) {

    String temp="测试数据!";

    URL url = new URL("http://trade.caimincun.com/servlet/NotifyDataServlet.do?");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    HttpURLConn.sendPost(url ,temp);

   }

}

   

 

二、HttpClient对象向服务器发送并接受数据

 

  测试类Test:

package *;

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

 

public class Test{

 public static void main(String[] args) {

  //定义客户端向url发送的参数1

  String transType ="参数1";
  //定义客户端向url发送的参数2
  String transMessage ="参数2";
  //定义需要访问的url链接

  String url = "http://trade.caimincun.com/servlet/HttpServlet.do";

  //定义http客户端对象--httpClient
  HttpClient httpClient = new HttpClient();
  //定义并实例化客户端链接对象-postMethod
  PostMethod postMethod = new PostMethod(url);
  // 设置http的头
  postMethod.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK"); 

  // 填入各个表单域的值

  NameValuePair[] data = { new NameValuePair("transType", transType),
    new NameValuePair("transMessage", transMessage) };
  // 将表单的值放入postMethod中

  postMethod.setRequestBody(data);
  
  // 定义访问地址的链接状态---eg:400,500
  int statusCode = 0;
  try {
   //客户端请求url数据
   statusCode = httpClient.executeMethod(postMethod);
  } catch (Exception e) {
   
   e.printStackTrace();
  }
  // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发

  // 301或者302
  if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  
   // 从头中取出转向的地址
   Header locationHeader = postMethod.getResponseHeader("location");
   String location = null;
   if (locationHeader != null) {
    location = locationHeader.getValue();
    System.out.println("The page was redirected to:" + location);
   } else {
    System.err.println("Location field value is null.");
   }
  } else {
   try {
    result_str = postMethod.getResponseBodyAsString();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  System.out.println(new Date(System.currentTimeMillis())+" "+"代理商返回信息:"+result_str);
  
  //释放链接
  postMethod.releaseConnection();
  httpClient.getHttpConnectionManager().closeIdleConnections(0);

  

 

 }

}

 

原创粉丝点击