如何使用 get 属性发送 http 请求? [英] How can I send http request with get attributes?

查看:29
本文介绍了如何使用 get 属性发送 http 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是我想向我的服务器发送一个 http 请求,例如:

What i mean is that i want to send an http request to my server like:

http://blahblah.blah/index.php?command=make

在安卓中

推荐答案

你可以这样做:

Http 帖子:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://blahblah.blah/index.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("command", "make"));        
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

Http 获取

HttpResponse response = null;
try {    
        // Create http client object to send request to server     
        HttpClient client = new DefaultHttpClient();
        // Create URL string
        String URL = "http://blahblah.blah/index.php?command=make";
        // Create Request to server and get response
        HttpGet httpget= new HttpGet();
        httpget.setURI(new URI(URL));
        response = client.execute(httpget);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } 
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

这篇关于如何使用 get 属性发送 http 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆