1

我需要使用同样使用基本身份验证的参数连接到一个 url。根据我的谷歌搜索,这就是我所拥有的:

String param = "?LS_ACCOUNT_SECRET=" + 
    URLEncoder.encode("dafsd","UTF-8") + 
    "&LS_ACCOUNT_SALT=" + URLEncoder.encode("dasfds","UTF-8");
url = new URL(com.jcs.toolbox.SitterMaticApi.API_REGISTER + param);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Authorization", 
    "Basic pass:word");
urlConnection.setRequestProperty("Content-Type", 
    "application/x-www-form-urlencoded");
OutputStreamWriter wr = new OutputStreamWriter(
    urlConnection.getOutputStream());
wr.write(param);
wr.flush(); 

// Get the response
responseInputStream = urlConnection.getInputStream();
rd = new BufferedReader(new InputStreamReader(
    urlConnection.getInputStream())); 
String line; 
while ((line = rd.readLine()) != null) { 
    acct +=  line;
}

//wr.close(); 
rd.close();
File acctfile = new File("maticacct.dat");
acctfile.createNewFile();
BufferedWriter buf = new BufferedWriter(new FileWriter(acctfile,true));
buf.append(acct);
buf.newLine();
buf.close();
new AlertDialog.Builder(this).setTitle("New Account").
    setMessage("New Account: " + acct).
    setNeutralButton("Close", null).show();

我尝试了 HTTPGet,但我得到“不允许获取方法”:

HttpGet request = new HttpGet(sb.toString());
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
InputStream content = response.getEntity().getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(content)); 
String line = null;
String acct = null;
while ((line = rd.readLine()) != null) { 
    acct +=  line;
}

rd.close();
content.close();

我已经添加了应用程序使用互联网的权限。所以任何帮助表示赞赏。

4

0 回答 0