2013-02-18 20 views
0

我需要創建一個帶有兩個參數的HttpPost請求。我同意有很多例子,但我相信我已經完成了我的研究,但我仍然沒有從ASP服務器獲得迴應。我試過NameValuePair,但我似乎無法獲得響應頁面。我猜測參數沒有被添加到httpPost對象。HttpPost參數無法工作到ASP服務器

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("https://portal.sibt.nsw.edu.au/Default.asp"); 

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2); 
nameValuePair.add(new BasicNameValuePair("userID", id.getText().toString())); 
nameValuePair.add(new BasicNameValuePair("password", pword.getText().toString())); 

post.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
HttpResponse response = client.execute(post); 

String responseContent = EntityUtils.toString(response.getEntity()); 
Log.d("response to string", responseContent); 

我又收到登錄頁面,這個代碼返回一個NullPointerException:

String newURL = response.getFirstHeader("Location").getValue(); 

我在做什麼錯在這裏?

+0

請提交的logcat期待着更多的幫助 – 2013-02-18 11:46:00

回答

0

我猜你只是錯過?在您的網址的結尾

0

嘗試這樣的事:

private static String urlAppendParams(String url, List<NameValuePair> lNameValuePairs) { 
    if (lNameValuePairs != null) { 
     List<NameValuePair> llParams = new LinkedList<NameValuePair>(); 
     for (NameValuePair nameValuePair : lNameValuePairs) { 
      llParams.add(nameValuePair); 
     } 

     String sParams = URLEncodedUtils.format(llParams, "UTF-8"); 
     if (!url.endsWith("?")) { 
      url += "?"; 
     } 
     url += sParams; 
    } 
    return url; 
} 
+0

感謝,這個曾與參數,可以,但我沒有得到任何迴應 EntityUtils.toString(response.getEntity())給我什麼。 – Sabbib 2013-02-20 12:15:55

+0

這可能是後端問題,或者您以錯誤的方式調用後端。 – sschrass 2013-02-20 12:56:01

+0

對不起,我是一名初學者,對於網站開發來說很新穎,請您澄清一下你的意思,也許告訴我如何以正確的方式去做。謝謝 – Sabbib 2013-02-21 01:46:08

0

當你使用瀏覽器(https://portal.sibt.nsw.edu.au/Default.asp)主頁到達時,服務器將打開會話(你可以看到設置cookie頭與網絡檢查員):

Set-Cookie:ASPSESSIONIDQCCCQQCQ=HJMMOCFAFILCLNCJIMNBACIB; path=/ 

也許你需要做一個GET請求,此頁面在POST登錄之前。另外,您必須在HttpClient中啓用cookie跟蹤。我通常使用:

HttpClient client = new DefaultHttpClient(); 
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); 
final HttpGet get = new HttpGet("https://portal.sibt.nsw.edu.au/Default.asp"); 
client.execute(get); //perform an initial GET to receive the session cookie 

//now do the post... 
HttpPost post = new HttpPost("https://portal.sibt.nsw.edu.au/Default.asp?Summit=OK"); 

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2); 
nameValuePair.add(new BasicNameValuePair("hcUserID", id.getText().toString())); 
nameValuePair.add(new BasicNameValuePair("hcPassword", pword.getText().toString())); 

post.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
HttpResponse response = client.execute(post); 

//I should be logged in.... Lets get the an internal webpage 
get = new HttpGet("https://portal.sibt.nsw.edu.au/std_Alert.asp"); 
client.execute(get); 

System.out.println(new String(get.getResponseBody())); 

由於我沒有有效的用戶/密碼,我無法嘗試此解決方案。

+0

我似乎無法找到setCookiePolicy()方法?也不是executeMethod()我只能找到的execute() 你可以嘗試使用該用戶名和密碼 用戶名chsac1203 密碼1234567k 請及時通知我儘快 – Sabbib 2013-02-20 12:18:48

+0

嗯,也許我在一箇舊的HttpClient API是說,我將更新代碼與新的API – lipido 2013-02-20 19:43:46

+0

我也嘗試過使用我的瀏覽器進行登錄/傳遞,並得到一條消息:「對不起,您的用戶ID或密碼不正確;或者您的門戶帳戶被禁用。」 – lipido 2013-02-20 19:54:06

0

您不使用.NET網頁,您必須創建.NET Web服務,因爲只有WebService具有發佈和獲取方法。你不要求網頁。網頁沒有收到您的信息和答案。

+0

這不是我的網頁,它是我的大學,還有什麼其他的選擇我有? – Sabbib 2013-02-20 12:16:53

+0

當你成功登錄時,你在URL文本中看到了什麼,如果你加密的文本不能做任何事情,因爲在你需要「網絡服務」之前我會解釋它。 – nurisezgin 2013-02-21 18:28:07

相關問題