ich eine Klasse erstellt haben, wo ich ein HttpURLConnection
Objekt bin die Schaffung und das Objekt auf eine andere Methode übergeben, wenn sie die folgende Bedingung passiert:java.lang.IllegalStateException: Bereits für setRequestProperty Methode verbunden: Java
In dieser Klasse Ich übergebe username
, password
und senderId
, um mit einer URL zu verbinden, die proxy
verwendet, wenn Verbindung nicht null ist.
Unten finden Sie vollständige Code für die Java-Klasse:
public class SMSGenerator {
public static HttpURLConnection sendSingleSMS(HttpURLConnection lconnection, String username,
String password, String senderId,
String mobileNo, String message) {
try {
String smsservicetype = "singlemsg"; // For single message.
String query = "username=" + URLEncoder.encode(username)
+ "&password=" + URLEncoder.encode(password)
+ "&smsservicetype=" + URLEncoder.encode(smsservicetype)
+ "&content=" + URLEncoder.encode(message) + "&mobileno="
+ URLEncoder.encode(mobileNo) + "&senderid="
+ URLEncoder.encode(senderId);
lconnection.setRequestProperty("Content-length", String.valueOf(query.length()));
lconnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
lconnection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
// open up the output stream of the connection
DataOutputStream output = new DataOutputStream(lconnection.getOutputStream());
// write out the data
int queryLength = query.length();
output.writeBytes(query);
// output.close();
// get ready to read the response from the cgi script
DataInputStream input = new DataInputStream(lconnection.getInputStream());
// read in each character until end-of-stream is detected
for (int c = input.read(); c != -1; c = input.read()) {
System.out.print((char) c);
}
input.close();
} catch (Exception e) {
System.out.println("Something bad just happened.");
e.printStackTrace();
}
return lconnection;
}
public static boolean sendSMS(String mobilenumber, String msg) throws MalformedURLException, IOException {
boolean flag = false;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxxx", xx));
URL surl = new URL("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
HttpURLConnection connection = null;
connection = (HttpURLConnection) surl.openConnection(proxy);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
HttpURLConnection.setFollowRedirects(true);
System.out.println("Response code " + connection.getResponseMessage());
System.out.println("code : " + connection.getResponseCode() + " " + HttpURLConnection.HTTP_NOT_FOUND);
if ("OK".equals(connection.getResponseMessage())) {
System.out.println("in");
SMSGenerator.sendSingleSMS(connection, "username", "password", "SMS", mobilenumber, msg);
// System.out.println("Sent successful SMS");
flag = true;
} else if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Hiiii");
} else if (connection.getResponseCode() == HttpURLConnection.HTTP_BAD_GATEWAY) {
System.out.println("Hi 1");
} else if (connection.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
System.out.println("Hi 2");
}
// System.out.println("");
return flag;
}
public static void main(String[] args) throws IOException {
new SMSGenerator().sendSMS("xxxxxxxxxx", "ManojYest1");
}
}
Das Problem mit diesem Code ist, dass, wenn ich überprüfen Sie die if
Zustand wie: connection != null
dann stellt er die Verbindung und ruft die Methode richtig. Wenn ich jedoch die Bedingung mit responseCode
als connection.getResponseCode() == HttpURLConnection.HTTP_OK
dann den folgenden Fehler überprüfen werden, treten auf:
java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3013)
Wie kann ich löse diese Fehler?
Nur die Ausnahme fangen. – CodeCody
haben bereits die Ausnahme nach dem Schließen des Eingabestreams eingefangen. Bitte überprüfen Sie –
versuchen, connect() vor dem Erhalten von Antwortcodes. – CodeCody