java post请求的错误。

starlover1999 2018-01-11 01:57:45
我发送一个POST请求。
https://dopen.weimob.com/fuwu/c/oauth2/token?code=CODE&grant_type=authorization_code&client_id=××&client_secret=××&redirect_uri=http://xxx.x.x.xx/Loan

CODE参数写死的时候没问题,但是用request.getParameter("")获取的code,替换CODE参数,发送post请求就报错。
...全文
863 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiongdejun 2018-01-11
  • 打赏
  • 举报
回复
引用 14 楼 starlover1999 的回复:
[quote=引用 13 楼 xiongdejun 的回复:] [quote=引用 12 楼 starlover1999 的回复:] [quote=引用 11 楼 xiongdejun 的回复:] 把这个问号去掉试下
去掉试过了,不行的[/quote] 你request.getParameter("")这个request作用域里边获取出来的值是不是 CODE ,如果不是这个值 ,你就让他变成这个值试试能不能通过。如果能通过那就是这个接口已经规定了这个参数必须是这个值了,其他值就会报错 [/quote] 我是用获取的code参数的值去替换CODE的,这个是成功的。不知道为什么发送请求的时候报错,我是在linux下面运行的。[/quote] qq1.4.0.1.0.7.2.7 交流吧 这个太慢了
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
引用 13 楼 xiongdejun 的回复:
[quote=引用 12 楼 starlover1999 的回复:] [quote=引用 11 楼 xiongdejun 的回复:] 把这个问号去掉试下
去掉试过了,不行的[/quote] 你request.getParameter("")这个request作用域里边获取出来的值是不是 CODE ,如果不是这个值 ,你就让他变成这个值试试能不能通过。如果能通过那就是这个接口已经规定了这个参数必须是这个值了,其他值就会报错 [/quote] 我是用获取的code参数的值去替换CODE的,这个是成功的。不知道为什么发送请求的时候报错,我是在linux下面运行的。
xiongdejun 2018-01-11
  • 打赏
  • 举报
回复
引用 12 楼 starlover1999 的回复:
[quote=引用 11 楼 xiongdejun 的回复:] 把这个问号去掉试下
去掉试过了,不行的[/quote] 你request.getParameter("")这个request作用域里边获取出来的值是不是 CODE ,如果不是这个值 ,你就让他变成这个值试试能不能通过。如果能通过那就是这个接口已经规定了这个参数必须是这个值了,其他值就会报错
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
引用 11 楼 xiongdejun 的回复:
把这个问号去掉试下
去掉试过了,不行的
xiongdejun 2018-01-11
  • 打赏
  • 举报
回复


把这个问号去掉试下
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
这个问题是在诡异,求各位大神指点迷津。
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
doget获取code值,这里能获取到code:
@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 用户同意授权后,能获取到code
		String code =request.getParameter("code").toString();
	
			
						WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken("", "", code);
						
						
						// 用户标识
						String openId = weixinOauth2Token.getOpenId();
						
						request.setAttribute("openId", "1");
						LoanJDBCDao.LoanJDBCDao.getInstance().test(openId);
						LoanJDBCDao.LoanJDBCDao.getInstance().test("1");
		
		// 跳转到index.jsp
		request.getRequestDispatcher("/index.jsp").forward(request, response);
		
	}
然后用获取的code替换链接中的CODE参数:
public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) {
		WeixinOauth2Token wat = null;
		
	
		String requestUrl = "https://dopen.weimob.com/fuwu/c/oauth2/token?";
		String	cc=	"code=CODE&grant_type=authorization_code&client_id=××&client_secret=××&redirect_uri=http://baidu.com";
		cc =  cc.replace("CODE",code);
		LoanJDBCDao.LoanJDBCDao.getInstance().test("CODE:"+code);
		// 获取网页授权凭证֤
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, cc, null);
		LoanJDBCDao.LoanJDBCDao.getInstance().test(jsonObject.toString());
				wat = new WeixinOauth2Token();
				
				wat.setOpenId(jsonObject.getString("openId"));
			
	
		return wat;
	}
然后调用httpsRequest请求:
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
		JSONObject jsonObject = null;
		LoanJDBCDao.LoanJDBCDao.getInstance().test(requestUrl);
		try {
			// 创建SSLContext对象,并使用我们信任的管理器初始化
			TrustManager[] tm = { new MyX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			// 从上述SSlContext对象中获取SSLSocketFactory对象
			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.setHostnameVerifier(new CommonUtil().new TrustAnyHostnameVerifier());
			conn.setSSLSocketFactory(ssf);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			// 设置请求方式(get/post)
			conn.setRequestMethod("POST"); 
			conn.connect();
			OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "utf-8");  
		    osw.write(requestMethod);  
		    osw.flush();  
		    osw.close();  
			LoanJDBCDao.LoanJDBCDao.getInstance().test(requestMethod);
			LoanJDBCDao.LoanJDBCDao.getInstance().test(requestUrl);
			
			// 从输入流读取返回内容
			int retCode = conn.getResponseCode();
			InputStream inputStream;
			if(retCode >=400)
			{
				inputStream=conn.getErrorStream();
			}
			else
				inputStream= conn.getInputStream();
			 
			//InputStream inputStream = conn.getInputStream();
			LoanJDBCDao.LoanJDBCDao.getInstance().test("2");
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}

			// 释放资源
			bufferedReader.close();
			inputStreamReader.close();
			inputStream.close();
			inputStream = null;
			conn.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (ConnectException ce) {
			log("连接超时{}"+ce);
		} catch (Exception e) {
			log("https请求异常:{}"+e);
			LoanJDBCDao.LoanJDBCDao.getInstance().test(e.toString());
		}
		return jsonObject;
	}
xiongdejun 2018-01-11
  • 打赏
  • 举报
回复
引用 7 楼 xiongdejun 的回复:
[quote=引用 6 楼 starlover1999 的回复:] [quote=引用 5 楼 xiongdejun 的回复:] 这个错误是url参数错误,既然有返回值,就说明你的请求是没有问题的,只是参数的问题,问一下你CODE参数写死的时候,request.getParameter("")获取的code不会报错?
code参数获取到了。获取到code后,然后我用获取的值去替换链接里面的code参数,就报错了。[/quote] 可以贴下你替换部分的代码吗[/quote] 为什么我总是感觉哪里怪怪的 你带吗贴出来我看下
xiongdejun 2018-01-11
  • 打赏
  • 举报
回复
引用 6 楼 starlover1999 的回复:
[quote=引用 5 楼 xiongdejun 的回复:] 这个错误是url参数错误,既然有返回值,就说明你的请求是没有问题的,只是参数的问题,问一下你CODE参数写死的时候,request.getParameter("")获取的code不会报错?
code参数获取到了。获取到code后,然后我用获取的值去替换链接里面的code参数,就报错了。[/quote] 可以贴下你替换部分的代码吗
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
引用 5 楼 xiongdejun 的回复:
这个错误是url参数错误,既然有返回值,就说明你的请求是没有问题的,只是参数的问题,问一下你CODE参数写死的时候,request.getParameter("")获取的code不会报错?
code参数获取到了。获取到code后,然后我用获取的值去替换链接里面的code参数,就报错了。
xiongdejun 2018-01-11
  • 打赏
  • 举报
回复
这个错误是url参数错误,既然有返回值,就说明你的请求是没有问题的,只是参数的问题,问一下你CODE参数写死的时候,request.getParameter("")获取的code不会报错?
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
引用 3 楼 lmx125254 的回复:
一般获取token的重定向域名需要编码,比如微信开发。你把重定向域名编码传上去试一下会不会再报错
我的redirect_uri用UTF-8编码了,还是不行
Leonis丶L 2018-01-11
  • 打赏
  • 举报
回复
一般获取token的重定向域名需要编码,比如微信开发。你把重定向域名编码传上去试一下会不会再报错
starlover1999 2018-01-11
  • 打赏
  • 举报
回复
错误信息:{"error":"invalid_grant","error_description":"Redirect URI mismatch."}
墨笙弘一 2018-01-11
  • 打赏
  • 举报
回复
贴错误信息啊

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧