使用HttpURLConnnection连接服务端,走到conn.getResponseCode();返回405,其他方法就没事

小哈的秘密城堡 2015-09-14 03:02:55
代码部分。个人感觉代码没啥错呀。一样的代码。
private String toRegist(String phone,String phoneCode,String password) {
HttpURLConnection conn = null;
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
try {
String newUrl = URL +"?phone="+ phone + "&phoneCode=" + phoneCode + "&password=" + password;
URL url = new URL(newUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");// 提交模式
conn.setRequestProperty("Content-Type", "plain/text; charset=UTF-8");

// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
// 根据ResponseCode判断连接是否成功
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
System.out.println("Not Success");
} else {
System.out.println("Success");
}
// 定义BufferedReader输入流来读取URL的ResponseData
bufferedReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
/* 关闭DataOutputStream */
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
conn.disconnect();
try {
if (printWriter != null) {
printWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return responseResult.toString();
}
...全文
590 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nergal_ 2015-09-21
  • 打赏
  • 举报
回复
引用 10 楼 u013682582 的回复:
[quote=引用 5 楼 e491288767 的回复:] 405:Method Not Allowed.说明你的的HTTP的POST方法错误。应该用get
我试了试吧参数直接不直接拼接上,而是用
printWriter = new PrintWriter(httpURLConnection.getOutputStream());  
            // 发送请求参数  
            printWriter.write(params.toString());  
            // flush输出流的缓冲  
            printWriter.flush();  
这样,每到返回操作码的时候都会是404,因为没辙所以才直接拼接在地址栏上,请问,你有啥好的办法么?[/quote] /** * 通过HttpURLConnection模拟post表单提交 * * @param path * @param params 例如"name=zhangsan&age=21" * @return * @throws Exception */ public static byte[] sendPostRequestByForm(String path, String params) throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");// 提交模式 // conn.setConnectTimeout(10000);//连接超时 单位毫秒 // conn.setReadTimeout(2000);//读取超时 单位毫秒 conn.setDoOutput(true);// 是否输入参数 byte[] bypes = params.toString().getBytes(); conn.getOutputStream().write(bypes);// 输入参数 InputStream inStream=conn.getInputStream(); return StreamTool.readInputStream(inStream); }
Sero_T 2015-09-15
  • 打赏
  • 举报
回复
这应该是网站解析时候的问题
nergal_ 2015-09-15
  • 打赏
  • 举报
回复
至于为什么其他地方能用我就不明白了,你有答案了,共享下
nergal_ 2015-09-15
  • 打赏
  • 举报
回复
405:Method Not Allowed.说明你的的HTTP的POST方法错误。应该用get
Sero_T 2015-09-15
  • 打赏
  • 举报
回复
换get方法吧!!!
nergal_ 2015-09-15
  • 打赏
  • 举报
回复
引用 2 楼 u013682582 的回复:
[quote=引用 1 楼 e491288767 的回复:] 你的URL后面的用户密码什么的是直接在action拼接的,这是get请求。自己搞清楚post和get的区别,百度吧,少年
其他方法我也用的这种拼接的,可以请求的。[/quote]
引用 2 楼 u013682582 的回复:
[quote=引用 1 楼 e491288767 的回复:] 你的URL后面的用户密码什么的是直接在action拼接的,这是get请求。自己搞清楚post和get的区别,百度吧,少年
其他方法我也用的这种拼接的,可以请求的。[/quote] 我只知道你这样写不符合HTTP协议规范。你打印下返回码是多少?
  • 打赏
  • 举报
回复
引用 5 楼 e491288767 的回复:
405:Method Not Allowed.说明你的的HTTP的POST方法错误。应该用get
我试了试吧参数直接不直接拼接上,而是用
printWriter = new PrintWriter(httpURLConnection.getOutputStream());  
            // 发送请求参数  
            printWriter.write(params.toString());  
            // flush输出流的缓冲  
            printWriter.flush();  
这样,每到返回操作码的时候都会是404,因为没辙所以才直接拼接在地址栏上,请问,你有啥好的办法么?
  • 打赏
  • 举报
回复
引用 5 楼 e491288767 的回复:
405:Method Not Allowed.说明你的的HTTP的POST方法错误。应该用get
还没解决,
  • 打赏
  • 举报
回复
引用 3 楼 e491288767 的回复:
[quote=引用 2 楼 u013682582 的回复:] [quote=引用 1 楼 e491288767 的回复:] 你的URL后面的用户密码什么的是直接在action拼接的,这是get请求。自己搞清楚post和get的区别,百度吧,少年
其他方法我也用的这种拼接的,可以请求的。[/quote]
引用 2 楼 u013682582 的回复:
[quote=引用 1 楼 e491288767 的回复:] 你的URL后面的用户密码什么的是直接在action拼接的,这是get请求。自己搞清楚post和get的区别,百度吧,少年
其他方法我也用的这种拼接的,可以请求的。[/quote] 我只知道你这样写不符合HTTP协议规范。你打印下返回码是多少?[/quote] 返回200的。
  • 打赏
  • 举报
回复
引用 1 楼 e491288767 的回复:
你的URL后面的用户密码什么的是直接在action拼接的,这是get请求。自己搞清楚post和get的区别,百度吧,少年
其他方法我也用的这种拼接的,可以请求的。
nergal_ 2015-09-14
  • 打赏
  • 举报
回复
你的URL后面的用户密码什么的是直接在action拼接的,这是get请求。自己搞清楚post和get的区别,百度吧,少年

80,355

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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