向服务器端发送数据 Get

Posted god-monk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了向服务器端发送数据 Get相关的知识,希望对你有一定的参考价值。

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

/**
     * * 向服务器端发送数据
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String urlGetMethod(String url, String params) throws Exception {
        HttpClient httpClient;
        GetMethod method = null;
        String responses;
        if (params != null) {
            // TODO 待调查
            if (url.indexOf("?") < 0) {
                url = url + "?";
            } else {
                url = url + "&";
            }
            url = url + URLEncoder.encode(params, "UTF-8");
        }
        try {
            httpClient = new HttpClient();
            method = new GetMethod(url);
            httpClient.executeMethod(method);
            responses = method.getResponseBodyAsString();//服务器返回内容,若服务器接收后没有返回内容为null
        } catch (Exception e) {
            throw e;
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
        return responses;
    }

模拟服务端获取数据

@SuppressWarnings("serial")
public class Export extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
         // 读取请求内容
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }

        String reqBody = sb.toString();
        System.out.println(reqBody);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}































































以上是关于向服务器端发送数据 Get的主要内容,如果未能解决你的问题,请参考以下文章

通过form向server端发送数据

使用 Dojo 文件上传客户端向 Java 服务器端发送附加数据

如何以 GET 方法将数据从客户端发送到服务器端 - ajax

求助android客户端传回的汉字参数,在服务器端出现乱码

如何防止从客户端向服务器端发送数据(位置、纬度和经度)作弊?

angularJS怎么实现与服务端的PHP进行数据交互