1. 程式人生 > >別人給我一個介面,是個url地址,讓我推送資料過去,然後得到別人返回的資料,怎麼呼叫這個介面呢?

別人給我一個介面,是個url地址,讓我推送資料過去,然後得到別人返回的資料,怎麼呼叫這個介面呢?

1:客戶端地址:http://1*2.***.**.1*4:7**8/peasentProducts/save(需要把服務端資料傳送到客戶端)

2:自己在控制層把資料放在放在一個map集合中,自己寫一個類封裝一個方法,把資料傳送過去

 

3:controller---控制層程式碼(需要捕獲異常)

在controller這層要把工具層的包引進來哦~~~~~

感覺很詳細了,哪裡不懂可以留言啊~~~~有錯還請指教~~~~~

try {
                //通過NpshYanZhengUtil類呼叫sendPut方法得到客戶端的返回資訊,
                String bd=NpshYanZhengUtil.sendPut("http://1*2.***.**.1*4:7**8/peasentProducts/save", map, "utf-8");
                //把資料轉化為json格式
                JSONObject ob = JSON.parseObject(bd);
                //獲取裡面的資料
                String ss = ob.getString("data");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

 

4:工具層---封裝的方法類

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public  class NpshYanZhengUtil  {
    
     public static String sendPut(String url, Map<String, Object> map, String encoding) throws ParseException, IOException {
            String body = "";
            CloseableHttpResponse response = null;
            //client 客戶端   ---------------------------- -----   // 建立預設的httpClient例項.
            CloseableHttpClient client = HttpClients.createDefault();
            System.err.println("client 客戶端:"+client.toString());
            System.err.println("client 客戶端:"+client);
            //PUT     - 向指定資源位置上傳其最新內容---------------// 建立httppost
            HttpPut httpPut = new HttpPut(url);
            //List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            JSONObject putData = new JSONObject();
            if (map != null && map.size() > 0) {
                //通過entrySet遍歷map裡面的值
                for (Entry<String, Object> entry : map.entrySet()) {
//                        nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
//                        httpPut.setHeader(entry.getKey(), entry.getValue());
                    if (entry != null && entry.getKey() != null && entry.getValue() != null) {
                        //把map裡面的鍵-值對資料存到putData裡面,同時轉化為json格式
                        putData.put(entry.getKey(), entry.getValue());
                    }
                }
            }
            //StringEntity是httpPut物件的一個實現類
            httpPut.setEntity(new StringEntity(putData.toString(), encoding));

            httpPut.setHeader("Content-type", "application/json");
            httpPut.setHeader("zx-Authentication", "");
//                httpPut.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //客戶端執行。傳送資料
            response = client.execute(httpPut);
            System.err.println("response:"+response);
            //通過response裡的getEntity()方法獲取客戶端的返回值,然後return進行返回;
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                body = EntityUtils.toString(entity, encoding);
            }
            //
            EntityUtils.consume(entity);
            response.close();
            System.err.println("body:"+body);
            return body;
        }  
}