end*_*rce 6 https post android login
我目前正试图通过Android将数据发布到我的网站.
我想发送数据的PHP脚本需要登录...
通过浏览器,我可以使用登录数据,如下面显示的链接:
HTTPS://演示:demo@www.example.com/foo.php巴= 42
如果我尝试相同的以下代码没有任何反应:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
String postUrl = "https://demo:demo@www.example.com/foo.php";
HttpPost httppost = new HttpPost(postUrl);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("bar", "42"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的唯一错误/响应:
"401 - 需要授权"
不幸的是我不知道如何解决这个错误):
感谢"Francesco Vadicamo"的工作代码:
public void postData() {
// Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
String postUrl = "https://www.example.com/foo.php";
HttpHost targetHost = new HttpHost("www.example.com", -1, "https");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("demo", "demo"));
HttpPost httppost = new HttpPost(postUrl);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("bar", "42"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(targetHost, httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这个:
HttpHost targetHost = new HttpHost(HOSTNAME, -1, SCHEME);
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(USERNAME, PASSWORD)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1619 次 |
| 最近记录: |