使用 httpClient 调用新浪微博接口

/**
 * 
 */
package httpClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.PartBase;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;

/** 
 * @ClassName: WeiboClient 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author zhoushun 
 * @date 2014年2月13日 上午11:09:34 
 *  
 */
public class WeiboClient {
	public static void main(String[] args) throws Exception
    {
        HttpClient client = new HttpClient();   
        //设置代理服务器地址和端口     
     //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
        //使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https
       // HttpMethod method = new GetMethod("http://10.1.14.20:8088/workflowController/service/todo/addTask"); 
        //使用POST方法
       // PostMethod method = new PostMethod("https://api.weibo.com/2/statuses/update.json"); 
        PostMethod method = new PostMethod("https://upload.api.weibo.com/2/statuses/upload.json"); 
      
        List<Header> headers = new ArrayList<Header>();
        headers.add(new Header("Authorization", "OAuth2 " + "2.00Tw_PoBdBYEND600e0aa317Sm_qoC"));
		headers.add(new Header("API-RemoteIP", "http://10.1.41.252"));
		client.getHostConfiguration().getParams()
				.setParameter("http.default-headers", headers);
        String s = "弗格森迭戈迭戈丰东股份的";
        File f = new File("E:\\Users\\zhoushun\\Downloads\\ce187bf40ad162d9f98819ea10dfa9ec8b13cdf6.jpg");
        byte b[] = new byte[(int) f.length()];
        FileInputStream fs = new FileInputStream(f);
        fs.read(b);
        fs.close();
        FileOutputStream fo = new FileOutputStream(new File("D://zs.jpg"));
        fo.write(b);
        fo.close();
    	// ((PostMethod) method).addParameter("status", s);
    	
    	 System.out.println(new MimetypesFileTypeMap().getContentType(f));
		HttpMethodParams param = method.getParams();
		param.setContentCharset("UTF-8");
     
	
		try {
			
			Part[] parts = new Part[2];
				parts[0] = new StringPart("status",URLEncoder.encode(s,"UTF-8"));
				FilePart filePart = new FilePart("pic", f.getName(),
						f, new MimetypesFileTypeMap().getContentType(f),
						"UTF-8");
				filePart.setTransferEncoding("binary");
				parts[parts.length - 1] = filePart;
				
//				parts[1] = new ByteArrayPart(b,
//						"123", "image/jpeg");
				method.setRequestEntity(new MultipartRequestEntity(parts,
					method.getParams()));
			

		} catch (Exception ex) {
			throw new Exception("dsad");
		}
		
		
     client.executeMethod(method);
        //打印服务器返回的状态
     System.out.println(method.getStatusLine());
       //打印返回的信息
     System.out.println();
     InputStream stream = method.getResponseBodyAsStream();
     
     BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
     StringBuffer buf = new StringBuffer();
     String line;
     while (null != (line = br.readLine())) {
         buf.append(line).append("\n");
     }
     System.out.println(buf.toString());
       //释放连接
     method.releaseConnection();
    }
	
	private static class ByteArrayPart extends PartBase {
		private byte[] mData;
		private String mName;

		public ByteArrayPart(byte[] data, String name, String type)
				throws IOException {
			super(name, type, "UTF-8", "binary");
			mName = name;
			mData = data;
		}

		protected void sendData(OutputStream out) throws IOException {
			out.write(mData);
		}

		protected long lengthOfData() throws IOException {
			return mData.length;
		}

		protected void sendDispositionHeader(OutputStream out)
				throws IOException {
			super.sendDispositionHeader(out);
			StringBuilder buf = new StringBuilder();
			buf.append("; filename=\"").append(mName).append("\"");
			out.write(buf.toString().getBytes());
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值