我怎么能发送带有获取属性的http请求?属性、我怎么能、http

2023-09-04 13:06:10 作者:打听爱情的下落

我的意思是,我想要一个HTTP请求发送到我的服务器,如:

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

HTTP:?//blahblah.blah/index.php 命令=使

在安卓

推荐答案

您可以不喜欢它:

HTTP POST:

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 GET

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
    }