原生的http请求

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());   //获取字符串

你可能感兴趣的:(原生的http请求)