-1

我想以字节格式将字符串数据 "" 发送到 android 应用程序中的服务器。我使用 HttpClient 但我认为它不是正确的方法,请帮助我如何做到这一点?

在 .net 的情况下,我想要 java 中的类似代码。

    string boundary = Guid.NewGuid().ToString();
    HttpWebRequest request = HttpWebRequest.Create(url)
        as HttpWebRequest;
    request.Method = "POST";
    //request.ContentType = "application/json";
    request.PreAuthenticate = true;

    byte[] fulldata = Encoding.UTF8.GetBytes(data);
    request.ContentLength = fulldata.Length;
    using (Stream sw = request.GetRequestStream())
    {
        sw.Write(fulldata, 0, fulldata.Length);
    }
4

1 回答 1

1

首先将您的字符串数据转换为字节,然后使用 ByteArrayEntity 以字节格式将数据发送到服务器。

试试这样

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.1/xxx");
HttpResponse response;
HttpParams hp = new BasicHttpParams();

//use ByteArrayEntity to send string data in byteformat
ByteArrayEntity byteEntity = new ByteArrayEntity(byte_data);
httppost.setEntity(byteEntity);
response = httpclient.execute(httppost); 
于 2012-12-13T13:45:42.980 回答