http post和get请求

来源:互联网 发布:英雄无敌6兵种数据 编辑:程序博客网 时间:2024/05/22 03:36
public static String HttpGetSend(String AddresURL) throws Exception {

int httpcode = 0;
String resposeStr = "";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

HttpClient hc = new HttpClient();
hc.setConnectionTimeout(10000);
hc.setTimeout(8000);
GetMethod pm = new GetMethod(AddresURL);
httpcode = hc.executeMethod(pm);
resposeStr = pm.getResponseBodyAsString();
pm.releaseConnection();
     
return resposeStr;

}

responseStr = HttpGetSend(supplierServerUrl);




public static String HttpPostSend(String AddresURL,NameValuePair[] nvp) throws Exception {

int httpcode = 0;
String resposeStr = "";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

HttpClient hc = new HttpClient();
hc.setConnectionTimeout(10000);
hc.setTimeout(8000);
PostMethod pm = new PostMethod(AddresURL);

pm.setRequestBody(nvp);
httpcode = hc.executeMethod(pm);
resposeStr = pm.getResponseBodyAsString();
pm.releaseConnection();
     
return resposeStr;

}

responseStr = HttpPostSend(supplierServerUrl, nvp);




//流post

public static String HttpPostSend(String AddresURL,String nvp) throws Exception {

int httpcode = 0;
String resposeStr = "";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

HttpClient hc = new HttpClient();
hc.setConnectionTimeout(10000);
hc.setTimeout(8000);
PostMethod pm = new PostMethod(AddresURL);
pm.addRequestHeader("Content-type",
"application/json; charset=GBK");

pm.setRequestBody(nvp);
httpcode = hc.executeMethod(pm);
resposeStr = pm.getResponseBodyAsString();
pm.releaseConnection();

System.out.println(AddresURL+"|"+httpcode);

return resposeStr;

}






int httpcode = 0;
String resposeStr = "";
HttpClient hc = new HttpClient();
hc.setConnectionTimeout(15000);
hc.setTimeout(15000);
PostMethod pm = new PostMethod(supplierServerUrl);
pm.setRequestBody(nvp);
httpcode = hc.executeMethod(pm);
InputStream is = pm.getResponseBodyAsStream(); 
            BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf-8")); 
            String line=null; 
            char[] c=new char[1024]; 
            int len=0; 
            while(-1 != (len = br.read(c))){ 
            line=new String(c, 0, len); 
            }
            responseStr=line;
pm.releaseConnection();
is.close();
br.close();

0 0
原创粉丝点击