Http post 发送图片+文字
精选
转载
- Map map = new HashMap();
- map.put("source", "appkey");
- map.put("status", txt);
- postImg("http://api.t.sina.com.cn/statuses/upload.json",map,Environment.getExternalStorageDirectory()+ "/temp.jpg"
- ,"帐号名字","密码");
-
-
-
-
-
-
-
-
- private void postImg(String actionUrl,Map<String, String> params, String filename,String username,String password) {
- try {
- String BOUNDARY = "--------------et567z";
- String MULTIPART_FORM_DATA = "Multipart/form-data";
- URL url = new URL(actionUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setUseCaches(false);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + ";boundary=" + BOUNDARY);
- String usernamePassword=username+":"+password;
- conn.setRequestProperty("Authorization","Basic "+new String(SecBase64.encode(usernamePassword.getBytes())));
- StringBuilder sb = new StringBuilder();
-
- for (Map.Entry<String, String> entry : params.entrySet()) {
- sb.append("--");
- sb.append(BOUNDARY);
- sb.append("\r\n");
- sb.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
- sb.append(entry.getValue());
- sb.append("\r\n");
- }
-
- DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
- outStream.write(sb.toString().getBytes());
- byte[] content = readFileImage(filename);
-
-
- StringBuilder split = new StringBuilder();
- split.append("--");
- split.append(BOUNDARY);
- split.append("\r\n");
- split.append("Content-Disposition: form-data;name=\"pic\";filename=\"temp.jpg\"\r\n");
- split.append("Content-Type: p_w_picpath/jpg\r\n\r\n");
- System.out.println(split.toString());
- outStream.write(split.toString().getBytes());
- outStream.write(content, 0, content.length);
- outStream.write("\r\n".getBytes());
- byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();
- outStream.write(end_data);
- outStream.flush();
- int cah = conn.getResponseCode();
-
- if(cah == 200)
- {
-
-
- new AlertDialog.Builder(Main.this)
-
- .setTitle("")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }})
- .setMessage(" 发布成功 ")
- .show();
- }
- else if(cah == 400)
- {
- new AlertDialog.Builder(Main.this)
-
- .setTitle("")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }})
- .setMessage(" 发布失败 \n 不可连续发布相同内容 ")
- .show();
- }else{
- throw new RuntimeException("请求url失败:"+cah);
- }
-
-
-
-
-
-
- outStream.close();
- conn.disconnect();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static byte[] readFileImage(String filename) throws IOException {
- BufferedInputStream bufferedInputStream = new BufferedInputStream(
- new FileInputStream(filename));
- int len = bufferedInputStream.available();
- byte[] bytes = new byte[len];
- int r = bufferedInputStream.read(bytes);
- if (len != r) {
- bytes = null;
- throw new IOException("读取文件不正确");
- }
- bufferedInputStream.close();
- return bytes;
- }
2、只发文字
-
- public void sendMsg(String status,String username,String password){
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://api.t.sina.com.cn/statuses/update.json");
-
- List params = new ArrayList ();
- params.add(new BasicNameValuePair("source", "4016954419"));
- params.add(new BasicNameValuePair("status", status));
- try
- {
-
- httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
- httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
- String data=username+":"+password;
- httppost.addHeader("Authorization","Basic "+new String(SecBase64.encode(data.getBytes())));
- httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
-
- HttpResponse httpResponse = new DefaultHttpClient().execute(httppost);
-
- if(httpResponse.getStatusLine().getStatusCode() == 200)
- {
-
-
- new AlertDialog.Builder(Main.this)
-
- .setTitle("")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }})
- .setMessage(" 发布成功 ")
- .show();
- }
- else if(httpResponse.getStatusLine().getStatusCode() == 400)
- {
- new AlertDialog.Builder(Main.this)
-
- .setTitle("")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }})
- .setMessage(" 发布失败 \n 不可连续发布相同内容 ")
- .show();
- }
- }
- catch (ClientProtocolException e)
- {
- e.printStackTrace();
- et.setText(et.getText()+" Error1:"+e.getMessage());
- }
- catch (IOException e)
- {
- e.printStackTrace();
- et.setText(et.getText()+" Error2:"+e.getMessage());
- }
- catch (Exception e)
- {
- e.printStackTrace();
- et.setText(et.getText()+" Error3:"+e.getMessage());
- }
- }