您的位置:首页 > 理论基础 > 计算机网络

原生的http请求

2016-02-18 17:29 483 查看
GET请求
String serverURL = "http://127.0.0.1/xxx/xx.jsp?username=abc;HttpGet httpRequest = new HttpGet(serverURL);// 建立http get联机HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 发出http请求if (httpResponse.getStatusLine().getStatusCode() == 200)String result = EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串
POST请求
 String uriAPI = "http://127.0.0.1/xxx/xx.jsp";  //声明网址字符串HttpPost httpRequest = new HttpPost(uriAPI);   //建立HTTP POST联机List <NameValuePair> params = new ArrayList <NameValuePair>();   //Post运作传送变量必须用NameValuePair[]数组储存params.add(new BasicNameValuePair("str", "I am Post String"));httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));   //发出http请求HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);   //取得http响应if(httpResponse.getStatusLine().getStatusCode() == 200)String strResult = EntityUtils.toString(httpResponse.getEntity());   //获取字符串
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: