I am writing a Java Program which could be able to send SMS using Kannel. I have Configured Kannel in my VM Vare Virtual Machine (Red Hat). Kannel is working fine and I can send SMS by typing the url
http://192.168.214.128:13013/cgi-bin/sendsms?
username=tester&password=foobar&to=03478847037&text=Mahtab
in my Windows browser. But when I access the same URL through Java Program
I am getting this exception
java.io.IOException: Server returned HTTP response code: 400` for URL:
http://192.168.214.128:13013/cgi-bin/sendsms?
username=tester&password=foobar&to=03478847037&text=Mahtab
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
But when I paste the same url string in browser I am able to send the SMS.
code is attached
URL url = new URL("http://192.168.214.128:13013/cgi-bin/sendsms?username=tester&password=foobar&to=03478847037&text=Mahtab");
System.out.println(param.toString());
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {answer.append(line);}
writer.close();
reader.close();
System.out.println(answer.toString());
Now please help me in this regard what I am missing???
I have solved this problem ............ actually code and every thing was right. The only problem was Netbeans. I had not cleaned the project when made some changes ...... that's why it was not giving the desired outcome ..... I cleaned the project and then build it... and I was successful so lesson learnt is that some time you are logically true but unfortunately your IDE is doing a little error which teases you the most.. #thor thanks for helping
Related
I have a script (script1.sh) in a unix machine that calls a other script (script2.sh) in other unix remote machine:
ssh user#machine /opt/.../script2.sh param1 param2
There is a trust relationship between both machines.
If I run the script2, it works correctly, but if I run the script1, it calls script2 but JAVA_HOME of script2 is lost. I know that I can fix by "set JAVA_HOME" in script2 but I prefer other solution that I don´t have to put the specific path of JAVA_HOME in each scripts that is called by script1 (script2, script3,...)
Any idea?
Regards.
I didn´t find the solution so I tried other way. The other way is thought Java.
script1 calls a Java Application that is in the same machine. This Java Application connect with the other machine and calls script2 and catchs the response. The code would be:
//With Try catchs
//Create connection
JSch jSSH = new JSch();
Session session = jSSH.getSession(user, server, port);
UserInfo ui = new SessionUser(password, null);//SessionUser implements UserInfo
session.setUserInfo(ui);
session.setPassword(password);
session.connect();
//Create channel:
Channel channel = session.openChannel("shell");
//Configure inputs and outputs of channel:
OutputStream inputstream_for_the_channel = channel.getOutputStream();
PrintStream commander = new PrintStream(inputstream_for_the_channel, true);
channel.setOutputStream(null);
channel.connect(100);
//Command to execute
commander.println("cd /.../scriptFolder; ./script2.sh param1 param2; exit");
commander.flush();
//System.out.println(channel.getExitStatus());
InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String line = null;
//Catch and print the response with a StringBuilder
//close connection
channel.disconnect();
session.disconnect();
How to check the this http response in selenium web driver? Is it possible to verify the same?
If you want to assert on the status of AJAX requests, then you can create a Browsermob proxy and use it's REST API to verify each request
Try this. It work for you :P
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String output;
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);}
Once done verify the response code(using asserts). Let me know if u still face issue.
I am trying to call HTTP request with network credential in blackberry. i have already implement on Java, Android it's working fine but not working on blackberry. Following step i have done in blackberry.
For set Network credential i have added three following jar.
commons-codec-1.6.jar
commons-httpclient-3.0.1.jar
commons-logging-1.1.1.jar
add this jar files are in blackberry project.
Following sample Code that work fine in Core Java.
try{
HttpClient client = new HttpClient();
GetMethod get = new GetMethod("http://www.google.com");
get.setDoAuthentication( true );
try {
int status = client.executeMethod( get );
System.out.println(status + "\n" + get.getResponseBodyAsString());
} finally {
get.releaseConnection();
}
}catch(Exception e){
System.out.println("Error:>>>>>>"+e);
}
Now there are not error on code but whenever try to click on application icon error face like "error starting appName: Module 'commons-httpclient-3.0.1' not found"
Can any one suggest what's this error say.
BB does not support HttpClient. But it does support J2ME's HttpConnection and is quite similar to HttpClient, so you can easily adjust with it. Here's some sample code to get you started:
try{
HttpConnection mConn = (HttpConnection)Connector.open(urlToPost);
mConn.setRequestMethod(HttpConnection.POST);
mConn.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
mConn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
mConn.setRequestProperty("Content-Language", "en-CA");
//---------------------------------------------------
mConn.setRequestProperty("User",usr);
mConn.setRequestProperty("pass",pass);
//---------------------------------------------------
catch(Exception e){//---handle your exceptions---//}
} finally {
mConn.close();//don't forget to close connections, only a limited number are available
}
This is a good article for beter understanding.
I am using the HTTP Connection in the following way:
HttpConnection _httpConnection = null;
try {
_httpConnection = (HttpConnection)Connector.open(_url);
} catch(Exception e) { }
byte [] postDataBytes = _postData.getBytes();
_httpConnection.setRequestMethod(HttpConnection.POST);
_httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
_httpConnection.setRequestProperty("Content-Language", "en-US");
_httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
_httpConnection.setRequestProperty("Connection", "close");
_httpConnection.setRequestProperty("Content-Length", Integer.toString(_postData.getBytes().length));
os = _httpConnection.openOutputStream();
os.write(postDataBytes);
os.flush();
This HTTP Connection requires parameters to successfully open. For example on a WIFI network, it requires the ";deviceside=true;interface=wifi" to be added to the URL.
The problem is for the EDGE connection. Each country requires different parameters to be added. For example in lebanon it requires ";deviceside=false" but in KSA if i add this parameter the connection will not open. In USA it needs different types of parametes. The question is how to establish an HTTP connection for all the countries with the same parameters. So that the application will successfully have an internet connection no matter where it is downloaded.
Welcome to the confusing world of network transports on BlackBerry! You will want to start with the article Connecting your BlackBerry - http and socket connections to the world.
Here is a simple example for "just give me a connection" (note, you will need to add appropriate error handling; also, myURL in the code below should have no connection descriptor info appended to it):
ConnectionFactory factory = new ConnectionFactory();
ConnectionDescriptor descriptor = factory.getConnection(myURL);
if (descriptor != null) {
_httpConnection = (HttpConnection) descriptor.getConnection();
...
}
Try using to use the method reffered in this link melick-rajee.blogspot.com and use it like
_url = "http://www.example.com";
_httpConnection = (HttpConnection)Connector.open(_url + getConnectionString());
You will have to sign the application to use this else the application will show exception.
To sign your application just go here Code Signing Keys
To use the connectionFactory, seems you need to set BisBOptions.
Try this:
connFact = new ConnectionFactory();
connFact.setTransportTypeOptions(TransportInfo.TRANSPORT_BIS_B,
new BisBOptions("mds-public"));
I am truing to integrate fusemail in asp.net 2.0. I am using HttpWebRequest for requesting the API pages. It has recently come to my notice that HttpWebRequest fails the first time and then continues and subsequent requests succeed.
say ( i know if i use goto it is a bad programming approach) if i use this code
retry:
try
{
Uri uri = new Uri("http://www.fusemail.com/api/request.html");
if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.PreAuthenticate = true;
request.Method =
WebRequestMethods.Http.Post;
//request.ReadWriteTimeout = System.Threading.Timeout.Infinite;
//request.Timeout = System.Threading.Timeout.Infinite;
request.ContentLength = data.Length;
request.ContentType =
"application/x-www-form-urlencoded";
//request.UserAgent = Request.UserAgent;
request.UserAgent = "Mozilla/4.0";
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = true;
//request.Accept = "Accept: text/html,application/xhtml+xml,application/xml";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
//Response.Write(tmp);
if (!String.IsNullOrEmpty(tmp))
{
return tmp;
}
}
return String.Empty;
}
catch (WebException ex)
{
goto retry;
}
it works after failing once. i am writing to a text file in case of an error and after i failed request it works the second time. I am using ASP.Net 2.0 and the website is hosted on IIS 7 with Windows Server 2008 Standard. Also pinging the API address it fails the first time and then responds
C:\>ping 67.207.202.118
Pinging 67.207.202.118 with 32 bytes of data:
Reply from 192.168.0.253: **Destination host unreachable**.
Reply from 67.207.202.118: bytes=32 time=218ms TTL=49
Reply from 67.207.202.118: bytes=32 time=218ms TTL=49
Reply from 67.207.202.118: bytes=32 time=217ms TTL=49
Ping statistics for 67.207.202.118:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 217ms, Maximum = 218ms, Average = 217ms
The first time it fails in HttpWebRequest it fails with this error
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 67.207.202.118:80
Is there an authentication issue the first time?. i read on some forums it first sends a 401 Unauthorized and then it can connect. I am unable to verify this using Fiddler.
Is there anything wrong with IIS configuration?
This is not a programming issue at all, I have faced a similar problem later and it was a network configuration problem due to ISA server / Firewall settings.
You have to contact your network administrator to check this issue.
I wish this helped you.
Yours,
Mohamed Kamal Elbeah
Senior .Net Developer
I recently came by this same issue. The solution in my case involved my testing environment, since I had multiple Ethernet adapters connected to my computer. While you may have a different IP for each of your Ethernet adapters, if they are all assigned to the same subnet this may cause a problem. The TCP connection is only attempted using one NIC at a time. So in my case, on the first try it would attempt the connection on one adapter that was not connected to my remote host, then on the second try it would connect using the second adapter which was connected. - Hope this helps someone.