我想以字节格式向android app中的服务器发送字符串数据“”。我使用的是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);
}
答案 0 :(得分: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);