使用HTTP的javacurl请求
我正在尝试使用Java执行一个CURL请求。CURL请求如下所示:使用HTTP的javacurl请求,java,curl,Java,Curl,我正在尝试使用Java执行一个CURL请求。CURL请求如下所示: String stringUrl = "https://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1"; URL url = new URL(stringUrl); URLConnection uc = url.openConnection(); uc.setRequestProperty("X-Requested-With", "C
String stringUrl = "https://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
int data = inputStreamReader.read();
char aChar = (char) data;
System.out.println(aChar);
curlhttps://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1 -u用户名:密码
我正在尝试执行以下请求:
String stringUrl = "https://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
int data = inputStreamReader.read();
char aChar = (char) data;
System.out.println(aChar);
我正在尝试查看inputStreamReader
的内容,如下所示:
String stringUrl = "https://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
int data = inputStreamReader.read();
char aChar = (char) data;
System.out.println(aChar);
代码编译和运行良好,但没有返回任何内容。我哪里出错了?我也在试着做那件事。我有一些解决办法,但它能看到所有东西 --这是密码---
--请注意,我使用此代码查看某个网站上是否存在某个帐户,因为它输出所有内容,所以我所做的是在代码上找到一个特定的规则,它可以告诉我该用户是否存在。嗯,我甚至不确定这是否能帮到你,但它可能是。祝你好运 我最终使用以下代码使其工作:
public static void main(String args[]) throws IOException {
String stringUrl = "url";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
StringBuilder html = new StringBuilder();
BufferedReader input = null;
try {
input = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String htmlLine;
while ((htmlLine = input.readLine()) != null) {
html.append(htmlLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
input.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(html.toString());
}
请参阅此问题:是否可以提供临时用户名和密码?在任何时候都有抛出异常吗?没有抛出异常,我将尝试设置临时访问。我查看了他们的站点文档,它要求开发人员首先获取API密钥,如so-
curlhttps://apis.sen.se/v2/user/api_key/ \-d“username=demoone”\-d“password=\u您的Sen.se\u帐户\u密码”
。您应该使用Authorization:token\u your\u token\u
在HTTPS请求上使用该令牌。你试过了吗?