新浪发布带图片的微博[multipart/form-data格式上传文件]

在使用新浪微博的API更新一条带图片的微博时,需要使用multipart/form-data风格的POST

可以参考这篇RFC http://www.ietf.org/rfc/rfc1867.txt

需要在request头设置Content-type

Content-type = multipart/form-data; boundary=xxxx

其中 boundary=xxxx很重要,是在request的body中构造分隔符的标识


下面这是使用httpfox截取的request的body部分,即post的数据部分

--xxxx
Content-Disposition: form-data; name="status"

111
--xxxx
Content-Disposition: form-data; name="pic"; filename="shell.png"
Content-Type: image/png

图片内容
--xxxx--

在xxxx前面加上两个’-‘构成了分隔行

在xxxx前后分别加上两个’-‘就构成了结束行

另外还要注意下,换行是'\r\n',而且正式内容和说明之间还隔了一个空行


下面是使用python上传带图片的微博

def requestMultiPart(self,URI,params):
		'''
			params =[{'name':'pic','filename':'shell.png','type':'image/png','data':'xxx'},...]
			
			if 'error' in return dict,thus the request was failed!
		'''
		if self.user_token==None or self.user_secret==None:
			return False,'token or secret is None' 
		header = [
        ('oauth_consumer_key', self.APP_KEY),
        ('oauth_nonce', uuid.uuid4().hex),
        ('oauth_signature_method', 'HMAC-SHA1'),
        ('oauth_timestamp', int(time.time())),
        ('oauth_version', '1.0'),
        ('oauth_token',self.user_token)
    	]
		header2 = header[:]
		header2.sort()
		p = 'POST&%s&%s' % (quote(URI, safe=''), quote(urlencode(header2)))
		signature = hmac.new(self.APP_SECRET + '&' + self.user_secret, p, hashlib.sha1).digest().encode('base64').rstrip()
		header.append(('oauth_signature', quote(signature)))
		header = ', '.join(['%s="%s"' % (k, v) for (k, v) in header])
		header = {'Authorization': 'OAuth realm="", %s' % header}
		header['Content-type'] ='multipart/form-data; boundary=huohua'
		
		data = ''
		for param in params:
			data += '--huohua\r\n'
			data += 'Content-Disposition: form-data; name="%s"; '%param['name']
			if 'filename' in param:
				data += 'filename="%s"'%param['filename']
			if 'type' in param:
				data += '\r\nContent-Type: %s'%param['type']
			data +='\r\n\r\n'
			data += param['data']
			data +='\r\n'
		data += '--huohua--'
		print data
		try:
			request = urllib2.Request(URI,data=data, headers=header)
			result = urllib2.urlopen(request).read().decode('utf-8')
			result = json.loads(result)
		except urllib2.HTTPError,e:
			result = {'error':str(e.code)+' ' + e.msg}
		except:
			result = {'error':'get respose from sina error: request error |'+traceback.format_exc()}
		
		return result


def statuses_upload(self,status,imgpath):
		'''
		upload image and update statuses
		'''
		if not os.path.exists(imgpath):
			return {'error':'image file not exist!'}
		filename = os.path.basename(imgpath)
		type = filename.split('.')[-1]
		f = open(imgpath,'rb')
		pic = f.read()
		print len(pic)
		f.close()
		requst = self.sinaRequest.requestMultiPart('http://api.t.sina.com.cn/statuses/upload.json',\
				[{'name':'status','data':status},{'name':'pic','filename':filename,'type':'image/'+type,'data':pic}])
		
		return requst


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用springboot做基础框架,提供简单页面做图片上传,接口直接模拟微博登陆,上传完成后返回图片保存链接 public String getSinaCookies() { String base64name = Base64Utils.encodeToString(username.getBytes()); String loginUrl = "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543"; String params = "entry=sso&gateway=1&from=null&savestate=30&useticket=0&pagerefer;=&vsnf=1&su;=" + base64name; params += "&service=sso&sp;=" + password + "&sr=1920*1080&encoding=UTF-8&cdult=3&domain=sina.com.cn&prelt=0&returntype=TEXT"; //登录 try { URL url = new URL(loginUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStream out = conn.getOutputStream(); out.write(params.getBytes()); out.flush(); out.close(); String sessionId = ""; String cookieVal = ""; String key = null; //取cookie for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) { if (key.equalsIgnoreCase("set-cookie")) { cookieVal = conn.getHeaderField(i); cookieVal = cookieVal.substring(0, cookieVal.indexOf(";")); sessionId = sessionId + cookieVal + ";"; } } if (sessionId != null) { String[] cookiearr = sessionId.split(";"); for (int i = 0; i < cookiearr.length; i++) { if (cookiearr[i].startsWith("SUB") && !cookiearr[i].startsWith("SUBP")) { sessionId = cookiearr[i] + ";"; } } } return sessionId; } catch (Exception e) { e.printStackTrace(); return null; } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值