当前位置: 首页 > news >正文

怎么用node做网站怎么快速优化关键词

怎么用node做网站,怎么快速优化关键词,WordPress 打赏插件,wordpress主题游戏cms目录 使用GET方法,访问GET接口,服务端返回405使用GET方法,访问POST接口,服务端返回405使用POST方法,访问GET接口,服务端返回405 使用GET方法,访问GET接口,服务端返回405 发生场景&a…

目录

  • 使用GET方法,访问GET接口,服务端返回405
  • 使用GET方法,访问POST接口,服务端返回405
  • 使用POST方法,访问GET接口,服务端返回405

使用GET方法,访问GET接口,服务端返回405

发生场景:
复制的POST请求代码,手动修改为GET,没有修改彻底,导致错误。

错误代码:

public class GET405 {public static void main(String[] args) {try {String defURL = "https://httpbin.org/get";URL url = new URL(defURL);// 打开和URL之间的连接HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("GET");//请求get方式con.setDoInput(true);// 默认值为 truecon.setDoOutput(true);//默认值为 false,传body参数必须写// 得到请求的输出流对象OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), "UTF-8");String body = "username=xiaohu&password=123456";writer.write(body);writer.flush();//            System.out.println("http请求方法:"+con.getRequestMethod());System.out.println("http状态码:" + con.getResponseCode());// 获取服务端响应,通过输入流来读取URL的响应InputStream is = con.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));StringBuffer sbf = new StringBuffer();String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("\r\n");}reader.close();// 关闭连接con.disconnect();// 打印读到的响应结果System.out.println("运行结束:" + sbf.toString());} catch (Exception e) {e.printStackTrace();}}
}

报错log:

405原因:
con.getOutputStream() 会把原有的GET方法改为POST方法,用POST方法访问GET接口,就报错405。看jdk1.8中HttpURLConnection的getOutpuStream()方法的源码:

解决办法:删掉getOutputStream(),用url传参。

正确代码:

public class GET200 {public static void main(String[] args) {try {String defURL = "https://httpbin.org/get";String body = "username=xiaohu&password=123456";URL url = new URL(defURL+"?"+body);// 打开和URL之间的连接HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("GET");//请求get方式//          System.out.println("http请求方法:"+con.getRequestMethod());System.out.println("http状态码:" + con.getResponseCode());// 获取服务端响应,通过输入流来读取URL的响应InputStream is = con.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));StringBuffer sbf = new StringBuffer();String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("\r\n");}reader.close();// 关闭连接con.disconnect();// 打印读到的响应结果System.out.println("运行结束:" + sbf.toString());} catch (Exception e) {e.printStackTrace();}}
}

运行结果:

http状态码:200
运行结束:{"args": {"password": "123456", "username": "xiaohu"}, "headers": {"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Host": "httpbin.org", "User-Agent": "Java/1.8.0_221", "X-Amzn-Trace-Id": "Root=1-668a1ba0-278338c97b93d6ca4276c0b0"}, "origin": "113.57.25.151", "url": "https://httpbin.org/get?username=xiaohu&password=123456"
}

使用GET方法,访问POST接口,服务端返回405

发生场景:接口文档显示接口为GET接口,实际上后端人员写的是POST接口,文档没同步。

错误代码:

public class GETtoPOST405 {public static void main(String[] args) {try {String defURL = "https://httpbin.org/post";String body="username=xiaohu&password=123456";URL url = new URL(defURL + "?" + body);HttpURLConnection con = (HttpURLConnection) url.openConnection();
//          con.setUseCaches(false); // Post请求不能使用缓存con.setRequestMethod("GET");//请求get方式con.setDoInput(true);// 设置是否从HttpURLConnection输入,默认值为 truecon.setDoOutput(false);// 设置是否使用HttpURLConnection进行输出,默认值为 falseint code = con.getResponseCode();System.out.println("http状态码:" + code);if (code == HttpURLConnection.HTTP_OK) {System.out.println("测试成功");} else {System.out.println("测试失败:" + code);}// 获取服务端响应,通过输入流来读取URL的响应InputStream is = con.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));StringBuffer sbf = new StringBuffer();String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("\r\n");}reader.close();// 关闭连接con.disconnect();// 打印读到的响应结果System.out.println("运行结束:" + sbf.toString());} catch (Exception e) {e.printStackTrace();}}
}

报错log:

http状态码:405
测试失败:405Caused by: java.io.IOException: Server returned HTTP response code: 405 for URL: https://httpbin.org/post?username=xiaohu&password=123456

405原因:不知道后端接口的定义,或者没有沟通彻底,或者后端开发人员失误,本应该是GET定义成了POST。应该使用POST方法。

解决方法:使用POST请求。

正确代码:

public class POSTtoPOST200 {public static void main(String[] args) {try {String defURL = "https://httpbin.org/post";URL url = new URL(defURL);HttpURLConnection con = (HttpURLConnection) url.openConnection();
//          con.setUseCaches(false); // Post请求不能使用缓存con.setRequestMethod("POST");//请求POST方式con.setDoOutput(true);// 设置是否使用HttpURLConnection进行输出,默认值为 falseOutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), "UTF-8");String body = "username=xiaohu&password=123456";writer.write(body);writer.flush();int code = con.getResponseCode();System.out.println("http状态码:" + code);if (code == HttpURLConnection.HTTP_OK) {System.out.println("测试成功");} else {System.out.println("测试失败:" + code);}// 获取服务端响应,通过输入流来读取URL的响应InputStream is = con.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));StringBuffer sbf = new StringBuffer();String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("\r\n");}reader.close();// 关闭连接con.disconnect();// 打印读到的响应结果System.out.println("运行结束:" + sbf.toString());} catch (Exception e) {e.printStackTrace();}}
}

运行结果:

http状态码:200
测试成功
运行结束:{"args": {}, "data": "", "files": {}, "form": {"password": "123456", "username": "xiaohu"}, "headers": {"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Content-Length": "31", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Java/1.8.0_221", "X-Amzn-Trace-Id": "Root=1-668a2091-2a64856935929fab74082ce4"}, "json": null, "origin": "113.57.25.151", "url": "https://httpbin.org/post"
}

使用POST方法,访问GET接口,服务端返回405

发生场景:代码失误,本该写GET,写成了POST。

错误代码:

public class POSTtoGET405 {public static void main(String[] args) {try {String defURL = "https://httpbin.org/get";URL url = new URL(defURL);// 打开和URL之间的连接HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("POST");//请求post方式
//            con.setUseCaches(false); // Post请求不能使用缓存con.setDoInput(true);// 设置是否从HttpURLConnection输入,默认值为 truecon.setDoOutput(true);// 设置是否使用HttpURLConnection进行输出,默认值为 falseOutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), "UTF-8");String body = "username=xiaohu&password=123456";writer.write(body);writer.flush();writer.close();int code = con.getResponseCode();System.out.println("http状态码:" + code);if (code == HttpURLConnection.HTTP_OK) {System.out.println("测试成功");} else {System.out.println("测试失败:" + code);}// 获取服务端响应,通过输入流来读取URL的响应InputStream is = con.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));StringBuffer sbf = new StringBuffer();String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("\r\n");}reader.close();// 关闭连接con.disconnect();// 打印读到的响应结果System.out.println("运行结束:" + sbf.toString());} catch (Exception e) {e.printStackTrace();}}
}

405原因: 接口只接受GET方法,请求是POST方法。

错误场景:后端开发定义失误,本该是POST接口,写成了GET。接口没有测试。

解决办法:用GET访问。正确代码和GET访问GET一样。

http://www.ritt.cn/news/17583.html

相关文章:

  • 外贸经济平台代销到哪里买苏州seo关键词优化价格
  • 视觉差 网站免费观看行情软件网站下载
  • wordpress多站批量发布手游推广平台有哪些
  • 深圳专业做网站建网站域名购买哪个网站好
  • 深圳外贸招聘效果好的关键词如何优化
  • php教育视频网站开发百度软件优化排名
  • 如何搜名字搜到自己做的网站营销成功的案例
  • 长沙市人才网seo批量建站
  • cms建站系统免费下载地图导航手机版免流量费用
  • 如何建设社交网站seo上海网站推广
  • 建设网站pptseo提升排名
  • 河北一建停考seo营销推广公司
  • 网站建设类别哈尔滨最新信息
  • 自建网站百度一下就知道官方
  • 怎么做查询数据输入的网站网络营销措施有哪些
  • 做网站充值犯法吗百度账号注册中心
  • 电商网站开发文字教程国家高新技术企业
  • 黄岩地区做环评立项在哪个网站免费推广app平台有哪些
  • 秒玩小游戏点击即玩入口seo网络推广企业
  • 广东做网站注册域名查询网站官网
  • 淘宝做网站找我要发票福州seo优化
  • 涉县手机网站建设公司品牌宣传方案
  • asp.net 4.0网站开发唯尚广告联盟app下载
  • 广州新塘网站制作推广百度指数功能
  • 网站广告条动画 怎么做网络推广的好处
  • 万网怎么建立网站微信管理助手
  • 域名服务商查询seo知识培训
  • wordpress发布的文章404seo站长工具综合查询
  • 韶关微网站建设网页广告
  • 哈密网站制作公司-哈密网站建设|哈密网络公司|哈密做网站长沙专业竞价优化公司