package com.android.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//sendHttp();
sendRequest("56.240.128.148",8045,"/dfsdf/login","user=admin&pwd=1");
}
private void sendHttp(){
String httpUrl = "http://56.240.128.148:8045/dfsdf/login";
HttpPost httpRequest = new HttpPost(httpUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "admin"));
params.add(new BasicNameValuePair("pwd", "1"));
try{
//设置字符集
HttpEntity httpentity = new UrlEncodedFormEntity(params, "GB2312");//"gb2312");
//请求httpRequest
httpRequest.setEntity(httpentity);
//取得默认的HttpClient
HttpClient httpclient = new DefaultHttpClient();
//取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//HttpStatus.SC_OK表示连接成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//取得返回的字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
Log.v("jkj","fsdfsdfsd");
}else{
Log.v("jkj","fsdfsdfsd");
}
}catch (ClientProtocolException e){
// resultText.setText(e.getMessage().toString());
} catch (IOException e){
// resultText.setText(e.getMessage().toString());
}catch (Exception e){
Log.v("jkj","fsdfsdfsd");
}
}
/////////////////////////////////////////////////////////////////////
private void sendRequest(String hostIp,int port,String cmd,String data){
Socket socket;
try {
socket = new Socket(hostIp, port);
OutputStream os = socket.getOutputStream();
// String data= getXmlString();
StringBuffer sb = new StringBuffer();
sb.append("POST "+cmd+" HTTP/1.1\r\n");// 注意\r\n为回车换行
// sb.append("Accept-Language: zh-cn\r\n");
// sb.append("Connection: Keep-Alive\r\n");
// sb.append("Host:localhost\r\n");
// sb.append("Content-Length:11\r\n");
sb.append("Host: "+hostIp+":"+port+"\r\n");
sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.append("Content-MD5: a781asdfadfbadsf1apoqst\r\n");//??????
sb.append("Accept-Encoding: gzip,deflate\r\n");
sb.append("Accept-Charset: GB2312\r\n");
sb.append("Keep-Alive: 115\r\n");
sb.append("\r\n");
sb.append(data+"\r\n");
//sb.append("\r\n");
// 接收Web服务器返回HTTP响应包
os.write(sb.toString().getBytes());
os.flush();
InputStream ins = socket.getInputStream();
byte[] b = new byte[1000];
ins.read(b); //读取http头
InputStreamReader ireader = new InputStreamReader(ins);
java.io.BufferedReader reader = new java.io.BufferedReader(ireader);
String s = reader.readLine(); //读取内容
System.out.println("response:"+s);
System.out.println(reader.readLine());
System.out.println(reader.readLine());
System.out.println(reader.readLine());
System.out.println(reader.readLine());
System.out.println(reader.readLine());
System.out.println(reader.readLine());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}