Java 带有auth的httpget请求
我尝试使用以下代码发出http get请求:Java 带有auth的httpget请求,java,android,http,Java,Android,Http,我尝试使用以下代码发出http get请求: String username = "test\\v100"; String host = "1.2.3.4"; String password = "pass"; HttpClient client = new DefaultHttpClient(); AuthScope as = new AuthScope(host, 90); UsernamePasswordCredentials upc = new UsernamePasswordCre
String username = "test\\v100";
String host = "1.2.3.4";
String password = "pass";
HttpClient client = new DefaultHttpClient();
AuthScope as = new AuthScope(host, 90);
UsernamePasswordCredentials upc = new UsernamePasswordCredentials(username, password);
((AbstractHttpClient) client).getCredentialsProvider().setCredentials(as, upc);
BasicHttpContext localContext = new BasicHttpContext();
BasicScheme basicAuth = new BasicScheme();
localContext.setAttribute("preemptive-auth", basicAuth);
HttpHost targetHost = new HttpHost(host, 90, "http");
HttpGet httpget = new HttpGet("/");
HttpResponse response = client.execute(targetHost, httpget, localContext);
但在最后一行出现异常“java.net.SocketException:Permission denied”
带有EclipseIDE的Android-2.2
主机系统中的Curl请求
curl -u test\v100:pass "http://1.2.3.4:90"
很好
如何以正确的方式发出http请求
谢谢 您是否在清单文件中添加了internet权限 如果没有,请在AndroidManifest.xml中添加此行:
<uses-permission android:name="android.permission.INTERNET" />