高分!我用HttpURLConnection模拟登陆网站,打印getHeaderField返回的数据,里面没有set-cookie?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高分!我用HttpURLConnection模拟登陆网站,打印getHeaderField返回的数据,里面没有set-cookie?相关的知识,希望对你有一定的参考价值。
用户名和密码没错。就是头文件里面的数据很少,没有cookie,是否是服务器的网站做了什么控制,还是发送请求需要怎么配置,求解答
参考技术A 好好检查递交的Header、数据,如refer是不是网站限制了等。。。。。最好做个简单的web server,检查自己递交的数据是不是正确的。追问请求头能写的都写了 Referer有。直接谷歌浏览器里面正常登陆看了提交的请求头。所以应该不会错。可返回的头文件数据里面就没有cookie
java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求,
方法一:
1 package main.utils; 2 3 import java.io.*; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 7 public class HttpUtilTest { 8 Log log = new Log(this.getClass());//初始化日志类 9 /** 10 * @作用 使用urlconnection 11 * @param url 12 * @param Params 13 * @return 14 * @throws IOException 15 */ 16 public String sendPost(String url,String Params)throws IOException{ 17 OutputStreamWriter out = null; 18 BufferedReader reader = null; 19 String response=""; 20 try { 21 URL httpUrl = null; //HTTP URL类 用这个类来创建连接 22 //创建URL 23 httpUrl = new URL(url); 24 //建立连接 25 HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); 26 conn.setRequestMethod("POST"); 27 conn.setRequestProperty("Content-Type", "application/json"); 28 conn.setRequestProperty("connection", "keep-alive"); 29 conn.setUseCaches(false);//设置不要缓存 30 conn.setInstanceFollowRedirects(true); 31 conn.setDoOutput(true); 32 conn.setDoInput(true); 33 conn.connect(); 34 //POST请求 35 out = new OutputStreamWriter( 36 conn.getOutputStream()); 37 out.write(Params); 38 out.flush(); 39 //读取响应 40 reader = new BufferedReader(new InputStreamReader( 41 conn.getInputStream())); 42 String lines; 43 while ((lines = reader.readLine()) != null) { 44 lines = new String(lines.getBytes(), "utf-8"); 45 response+=lines; 46 } 47 reader.close(); 48 // 断开连接 49 conn.disconnect(); 50 51 log.info(response.toString()); 52 } catch (Exception e) { 53 System.out.println("发送 POST 请求出现异常!"+e); 54 e.printStackTrace(); 55 } 56 //使用finally块来关闭输出流、输入流 57 finally{ 58 try{ 59 if(out!=null){ 60 out.close(); 61 } 62 if(reader!=null){ 63 reader.close(); 64 } 65 } 66 catch(IOException ex){ 67 ex.printStackTrace(); 68 } 69 } 70 71 return response; 72 } 73 }
方法二:使用httpclient实现
1 //post请求方法 2 public String sendPost(String url, String data) { 3 String response = null; 4 log.info("url: " + url); 5 log.info("request: " + data); 6 try { 7 CloseableHttpClient httpclient = null; 8 CloseableHttpResponse httpresponse = null; 9 try { 10 httpclient = HttpClients.createDefault(); 11 HttpPost httppost = new HttpPost(url); 12 StringEntity stringentity = new StringEntity(data, 13 ContentType.create("text/json", "UTF-8")); 14 httppost.setEntity(stringentity); 15 httpresponse = httpclient.execute(httppost); 16 response = EntityUtils 17 .toString(httpresponse.getEntity()); 18 log.info("response: " + response); 19 } finally { 20 if (httpclient != null) { 21 httpclient.close(); 22 } 23 if (httpresponse != null) { 24 httpresponse.close(); 25 } 26 } 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 return response; 31 }
以上是关于高分!我用HttpURLConnection模拟登陆网站,打印getHeaderField返回的数据,里面没有set-cookie?的主要内容,如果未能解决你的问题,请参考以下文章
Android利用HttpURLConnection实现模拟登录
转Java模拟http请求,调用外部api接口:HttpURLConnection和HttpClient的区别