我正在尝试从服务器获取文本文件,但是出现上述错误,有人知道如何解决它?
当我通过POSTMAN执行GET请求时,会收到200条响应。
,现在堆栈溢出说:“您的帖子似乎主要是代码;请添加更多详细信息。”这就是为什么我要添加这一行,并希望这个愚蠢的算法可以让我和平地发表我的问题
更新: 即使使用jenkins插件“ http request”,我仍然收到200条消息,这是否意味着我的代码有问题?
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
import java.lang.Object;
import org.apache.commons.codec.binary.Base64;
import javax.net.ssl.HttpsURLConnection;
public class httpClient {
public static void sender() {
try {
File file = new File("file.txt");
FileReader input = new FileReader(file);
BufferedReader bufRead = new BufferedReader(input);
String Line=null;
String[] array1= new String[5000];
int count=0;
while ( (myLine = bufRead.readLine()) != null)
{ count++;
array1[count] = Line;
}
String webPage = "URL";
String name = "usr";
String password = "pass";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(webPage);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Type", "text/plain");
urlConnection.setRequestProperty("Accept", "text/plain");
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
////////////////////////////////////////////////////////
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int number;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((number = isr.read(charArray)) > 0) {
sb.append(charArray, 0, number);
}
String result = sb.toString();
int code = urlConnection.getResponseCode();
System.out.println(code);
System.out.println(result);
}catch (IOException e) {
e.printStackTrace();
}
}
}