2016-02-07 203 views
0

我尝试从名为alphadraft.com的网站上浏览一些数据。不幸的是,我需要对自己进行身份验证才能访问这些数据。在搜索了一段时间之后,我了解到我需要向登录我的网站发送POST请求。通过Chrome DevTools,我了解到this应该是我登录的发布请求。通过java登录网站

After搜索如何做到这一点,我偶然发现了这个答案,并决定复制我的目的。 How to send Request payload to REST API in java?

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.HttpURLConnection; 
import java.io.OutputStreamWriter; 


public class testasdf { 

    public static void main(String[] args) throws IOException, NumberFormatException { 

      String line; 
      StringBuffer jsonString = new StringBuffer(); 
      try { 

       URL url = new URL("https://alphadraft.com/api/ui/auth/loginuser/"); 

       //escape the double quotes in json string 
       String payload="{email_address : \"(EMAILHERE)\", password : \"(PASSWORDHERE)\"}"; 
       System.out.println(payload); 

       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

       connection.setDoInput(true); 
       connection.setDoOutput(true); 
       connection.setRequestMethod("POST"); 
       connection.setRequestProperty("accept", "*/*"); 
       connection.setRequestProperty("content-type", "text/plain;charset=UTF-8"); 
       connection.setRequestProperty("user-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); 
       OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); 
       writer.write(payload); 
       writer.close(); 
       BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
       while ((line = br.readLine()) != null) { 
         jsonString.append(line); 
       } 
       br.close(); 
       connection.disconnect(); 
       System.out.println(jsonString.toString()); 
      } catch (Exception e) { 
        throw new RuntimeException(e.getMessage()); 
      } 
      URL url1 = new URL("https://alphadraft.com/api/ui/contest/getloadinfo/"); 
      URLConnection con1 = url1.openConnection(); 
      con1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36"); 
      con1.connect(); 
      InputStream is1 =con1.getInputStream(); 
      BufferedReader br1 = new BufferedReader(new InputStreamReader(is1)); 


       System.out.println(br1.readLine()); 
     } 
    } 

然而,错误消息

{ “错误”: “无法登录”} { “错误”: “未经过身份验证”}

仍然弹出为我的输出。如果有人能解释我为什么不行,我会很感激。

+0

您还没有发布格式正确的JSON(假设REST API接受JSON) –

+0

相关问题: http://stackoverflow.com/questions/2285250/how-to-log-into-facebook-programmatically-using-java –

回答

0

JSON格式requiters引号既为属性名称和值所以你的情况应该是: String payload="{\"email_address\" : \"(EMAILHERE)\", \"password\" : \"(PASSWORDHERE)\"}";

+0

非常感谢,你是绝对正确的。登录部分现在编译好,但我仍然没有通过身份验证,当我尝试去网站上,登录是。有任何想法吗? – Heytherewhatsup

+0

现在我确实已经弄清楚了,我需要从POST请求的响应中获取set-cookie标头,并在另一页的GET请求中使用它。不管怎么说,还是要谢谢你。 - 线程闭包 - – Heytherewhatsup