Sunday 30 April 2017

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found




I have some typical codes which used HttpURLConnection to get a file with an URL.
They worked fine in android 1.x and 2.x. But failed in Android 4.1!



I searched on the web but found little similar information.
Would anybody please help to investigate this issue?



private String mURLStr; 
private HttpURLConnection mHttpConnection;

...


url = new URL(mURLStr);

...

mHttpConnection = (HttpURLConnection) url.openConnection();
mHttpConnection.setDoOutput(true);
mHttpConnection.setRequestMethod("GET");

...


InputStream is = mHttpConnection.getInputStream();


The getInputStream method throws an exception:



08-01 15:56:48.856: W/System.err(13613): java.io.IOException: No authentication challenges found
08-01 15:56:48.856: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)

08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
...

Answer



I am currently facing the same problem. On 4.1 Jelly Bean I receive an IOException "No authentication challenges found" when calling getResponseCode() on the HttpURLConnection.



I have searched online to see what has changed in the Android source code and found the following:
4.0.4 (working): https://bitbucket.org/seandroid/libcore/src/7ecbe081ec95/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
4.1.1 (not working): https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java




As one can see in 4.1 JB the method getAuthorizationCredentials() throws the IOException. It parses the challenge headers it finds in the response using HeaderParser.parseChallenges(..), if the response code is 401 or 407. If the returned List is empty the Exception is thrown.



https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HeaderParser.java



We are currently investigating what exactly causes that List to be empty, but have the suspicion that our server might use realm=... instead of realm="..." in the challenge header. Missing quotation marks might be the cause for this problem. We have to investigate further if that is indeed the case and if we can make it work.


How do I copy to the clipboard in JavaScript?

What is the best way to copy text to the clipboard? (multi-browser)



I have tried:




function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData("Text", text);
} else {
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(text);
}
}



but in Internet Explorer it gives a syntax error. In Firefox, it says unsafeWindow is not defined.



A nice trick without flash: How does Trello access the user's clipboard?

jquery - unable to get the results from the yahoo finance api for my site due cross domain funcationality





if (window.XMLHttpRequest)
{

xmlhttp=new XMLHttpRequest();

}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://finance.yahoo.com/d/quote.csve=.csv&s=^BSESN&f=nl1c2vgh&random=10",true);

xmlhttp.send(null);

xmlhttp.onreadystatechange=function()

{

if (xmlhttp.readyState==4)
{
window.alert(xmlhttp.status);
}
}


The above code showing status 200 in IE But in Firefox and Chrome got the status 0 due cross domain functionality.

Can any one help to how to overcome this cross domain functionality using java script.


Answer



Direct call to other domain is not possible from JavaScript ajax due to cross domain issue best way to do this type of call is call your own webpage using Ajax and from your server side script call these apis and get answer and return to your ajax call.



if you are using php you can use cURL OR file_get_contents to fetch content from url


java - What is the alternative for String.contains method that is case insensitive?




While reading the line as a string from a file and string.contains("someexamplestring") will return the output of the case sensitive string.



If there is "someExampleString" in line, it's not returning.



How to identify a string in a case-insensitive manner?


Answer



Actually, this is a duplicate of How to check if a String contains another String in a case insensitive manner in Java?







If you've simpler requirements and are dealing with English letters only, you can follow the below answer.



You should do string.toLowerCase().contains("someExampleString".toLowerCase());.



Read more about public String toLowerCase() from Java SE Documentation.



Also, as hinted by Artur Biesiadowski in the comment section of the question, re-iterating it here :





Regarding all the answers suggesting toLowerCase/toUpperCase - be
careful if you go outside of ASCII space
. There are some languages
where going lower to upper and back (or other way around) is not
consistent. Turkish with its dotless 'i' comes to mind : Dotted and
dotless I








Also, to make it safer, you may use another method toLowerCase(Locale.English) and override the locale to English always. But, the limitation being you are not internationalized any longer.



string.toLowerCase(Locale.English).contains("someExampleString".toLowerCase(Locale.English));

mysql - Can trim() and strip_tags() prevent sql injection in php?

Is using trim() and strip_tags() for preventing sql injection is as okay as PDO or Prepaid Statement?
thre is another question on how to prevent sql injection. trim() and strip_tags() was not even discussed there.

I dont know from where people saying duplicate finding about trim() and strip_tags() in the former question answer!? Go read the answers and my question again



     $username = strip_tags(trim($_POST["username"]));
$password = strip_tags(trim($_POST["password"]));

javascript - Is checking for the readiness of the DOM overkill?



I'm developing a platform for developing desktop apps with web technologies. In the course of doing so I've been trying to get some document/on-ready functionality working with the browser I will be integrating into the platform. That's why I'd previously asked this this question here on SO: javascript-framework-that-primarily-provides-just-document-onready-functionality



However I've not been able to get my browser of choice (shush, its a secret ;) to successfully utilize the functionality suggested by the one and only answer to the above. So, in the course of just trying to figure out what might possibly work I stumbled upon the following.




The code below has the same effect within this browser I'm using simply by executing a function after a timeout of 1 millisecond: I can write to the DOM while the big image is loading. This might not be the ultimate solution for me, I may write something specific to how DOM functionality is implemented by the Javascript engine for this browser.



Nevertheless, I decided to see if this works in standard browsers, and much to my surprise, it does! In light of which, my question: are various implementations of dom/readiness functionality provided by various Javascript frameworks, simply overkill?



"http://www.w3.org/TR/html4/loose.dtd">


Untitled Document














EDIT/FURTHER THOUGHTS
On the page linked to by the answer to my previous related question it states "For Firefox and Opera a simple check of event type will determine if it is DOMContentLoaded. Safari and IE will check against the document’s ready state....Finally in case all else fails, the onload event will bring up the rear." Perhaps a setInterval similar to my setTimeout above could be the penultimate course of action, before relying on onload as a last resort? In any event, with the embeddable browser I've chosen, neither the DOMContentLoaded event, nor document.readyState appear to be supported.


Answer




Your hunch is good and well founded IMO. But someone has beat you to the punch already. Short answer is that setTimeout is not a working implementation of detecting DOM readiness in all cases. It may be ok for your browser of interest, but IE fails in some situations.



It may interest you to know that Microsoft's own ASP.NET AJAX framework uses the setTimeout trick to detect DOM readiness. And surprise, surprise: it fails in certain use cases as well.



In short, the problem seems to lie in IE with slow loading scripts, either due to large file size (eg ~500K) or network/server latency.


c# - Automatically exit generated Excel file correctly

My program generates an Excel in a loop and sends it by Email. In each loop iteration it should overwrite the Excel, but the Excel is still used by the old Excel process which is why I get a System.IO.IOException exception.



Here is the process:



enter image description here



Here is the Code:




if(File.Exists(PfadXlsx)) File.Delete(PfadXlsx);

mExcel.Visible = true;
mExcelWs.SaveAs(PfadXlsx, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, missing, missing, false, false, false, missing, missing, missing); // Datei speichern

mExcelWb.Close(true, missing, missing);
mExcel.Application.Quit();
mExcel.Quit();




mExcel == Excel.Application mExcelWB == Excel.Workbook
mExcel==Excel.Worksheet missing == type.missing


Get Cell reference for TOP 3 numbers within a Range (Excel VBA)



I have a Range of Cells in Excel with numbers (Let's say A1:Z1) and I want to get three highest numbers. Answer to this part of the question I found here - Finding highest and subsequent values in a range



But I want also to get the cell reference of these values.



firstVal = Application.WorksheetFunction.Large(rng,1)
secondVal = Application.WorksheetFunction.Large(rng,2)

thirdVal = Application.WorksheetFunction.Large(rng,3)

Answer



After getting the values, try looping through the range and assign range variables to these. Then print the addresses of the range variables:



Sub TestMe()

Dim firstVal As Double
Dim secondVal As Double
Dim thirdVal As Double

Dim rng As Range
Set rng = Worksheets(1).Range("A1:B10")

With Application
firstVal = Application.WorksheetFunction.Large(rng, 1)
secondVal = Application.WorksheetFunction.Large(rng, 2)
thirdVal = Application.WorksheetFunction.Large(rng, 3)
End With

Dim myCell As Range

Dim firstCell As Range
Dim secondCell As Range
Dim thirdCell As Range

For Each myCell In rng
If myCell.Value = firstVal And (firstCell Is Nothing) Then
Set firstCell = myCell
ElseIf myCell.Value = secondVal And (secondCell Is Nothing) Then
Set secondCell = myCell
ElseIf myCell.Value = thirdVal And (thirdCell Is Nothing) Then

Set thirdCell = myCell
End If
Next myCell

Debug.Print firstCell.Address, secondCell.Address, thirdCell.Address

End Sub


The check firstCell Is Nothing is done to make sure that in case of more than one top variable, the second one is assigned to the secondCell. E.g., if the range looks like this:




enter image description here



then the top 3 cells would be A2, A3, A1.


javascript - Is it bad practice declaring global variables in a.js file?



I have a .js file where I am initialising two parameters which are used
in a seperate function :



var submyvar1;
var submyvar2;


function init(myvar1 , myvar2){
submyvar1= myvar1;
submyvar2= myvar2;
}

function (){
//subvar1 & subvar 2 used here
}



Is declaring global variables like this a bad practice ?



If so, what is the alternative, wrap the entire .js file in an object ?


Answer



At least it's not a good practice, you could use an immediated invoked function expression:



(function() {
var x;
var y;


window.init = function(xValue, yValue) {
x = xValue;
y = yValue;
}

window.doWhateverYouWant = function() {
console.log(x + y);
}
}());



You could use this pattern unless you need access your global variable outside this .js file, when an cross-accessing between .js files or