how to upload a audio file using HTTP POST from blackberry 5.0? - audio

i need to upload a audio file stored in phone memory using http post. what steps required for it. any helps will be appreciated.
i doing the client side progrmming.
InputStream inputStream = null;
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
HttpConnection httpConn;
httpConn = (HttpConnection)connDesc.getConnection();
httpConn.setRequestMethod(HttpConnection.POST);
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
//here i want to append the audio data from phone memmory . how it will be done?
thanks

There is an example of using the HTTP POST protocol in the API documentation for HttpConnection. You will, as PA suggests, have to provide an HTTP server to accept the conection and do something with the data.

Related

Socket handle leak happening on Azure Web App

I have been stuck in one issue. I am getting error of "Unable to connect to the remote server. An attempt was made to access a socket in a way forbidden by its access permissions".
After searching on web and taking help of azure support, I came to know that if Web App reaches outbound connection limit of azure web app instance, it refuses connections or kill extra connections. Here is the image of open socket handles
My application calls third party WebAPI and wcf service. I have written code to close connections after making call to APIs. but it doesn't work for me. I did following code to call Web API.
var request = (HttpWebRequest)WebRequest.Create("www.xyz.com");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = false;
request.Headers.Add("Authorization", "Handshake");
byte[] bodyData;
bodyData = Encoding.UTF8.GetBytes(input_data);
request.ContentLength = bodyData.Length;
request.GetRequestStream().Write(bodyData, 0, bodyData.Length);
request.GetRequestStream().Flush();
request.GetRequestStream().Close();
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream(),
Encoding.UTF8))
{
string output_data = reader.ReadToEnd();
}
response.Close();
}
Could anyone guide me how to get rid on this issue?

How to create attachments in couchDB using RestAPI java client

I have a requirement to store attachment on CouchDB. How will I do it using Java. What APIs I can use? Is there any sample code or documentation I can refer? Currently we are using cloudantdb, but we planing to use CouchDB on Azure
com.cloudant.client.api.Database;
com.cloudant.client.api.model.Response;
Thanks
If you use LightCouch Java API
InputStream in = // .. init stream
// save under a new document, a generated UUID is assigned as id.
Response response = dbClient.saveAttachment(in, "photo.png", "image/png");
// save to an existing document
dbClient.saveAttachment(in, "file.pdf", "application/pdf", "doc-id", "doc-rev");
// save under a new document with the given id.
dbClient.saveAttachment(in, "photo.jpeg", "image/jpeg", "doc-id", null);
// get attachment
InputStream in = dbClient.find("doc-id/photo.jpeg");
// ..
in.close(); // close the stream

HTTP :connect timeout and read timeout for URLStreamHandler with SAAJ working for windows but not working on linux sytem

I'm a beginner when it comes to HTTP connections.
Currently i'm working with SAAJ api to have my soap based client, where to handle timeouts i ended up using URLStreamHandler for HTTP connection properties with the endpoints.
Problem is that this timeout works for my windows based system, however it isn't working for the Linux server it is going to go live on.
below is the code for fetching endpoint with set properties. It is a HTTP POST connection.
URL endpoint = new URL (null, url, new URLStreamHandler () {
protected URLConnection openConnection (URL url) throws IOException {
// The url is the parent of this stream handler, so must create clone
URL clone = new URL (url.toString ());
HttpURLConnection connection = (HttpURLConnection) clone.openConnection();
connection.setRequestProperty("Content-Type",
"text/xml");
connection.setRequestProperty("Accept",
"application/soap+xml, text/*");
// If we cast to HttpURLConnection, we can set redirects
// connection.setInstanceFollowRedirects (false);
connection.setDoOutput(true);
connection.setConnectTimeout(3 * 1000);
connection.setReadTimeout(3 * 1000);
return connection;
for the SAAJ API part, below is the implementation, pretty basic one
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
.newInstance();
soapConnection = soapConnectionFactory.createConnection();
is = new ByteArrayInputStream(command.getBytes());
SOAPMessage request = MessageFactory.newInstance(
SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(
new MimeHeaders(), is);
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("Content-Type", "text/xml");
request.saveChanges();
ByteArrayOutputStream out = new ByteArrayOutputStream();
request.writeTo(out);
soapResponse = soapConnection.call(request, endpoint);
Is it that system properties would affect connect or read timeout. If so please let me know what could cause this behavior.

How can I download file with HTTPBuilder (HttpClient)?

I need to download and save file. I'm trying to use HTTPBuilder because it has simple API and supports cookies. I have written following code:
//create new httpBuilder and set cookies
def httpBuilder = ...
def file = ...
def inputStream = httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY)
FileUtils.copyInputStreamToFile(inputStream)
How can I check that file is correctly downloaded (not only the part of the file)?
For large files exception java.lang.OutOfMemoryError: Java heap space occurs on line def inputStream = httpBuilder.get... How can I solve it?
May be it's not best choise to download files by HTTPBuilder. What is the best way to download file with cookies support?
Have you tried HttpBuilder GET request with custom response-handling logic:
httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY) {
resp, inputStream ->
FileUtils.copyInputStreamToFile(inputStream)
}
If HttpBuilder has problems which is strange then you can always use the tried and true Apache HttpClient API which has full cookie support.
HttpGet req = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(req);
// validate response code, etc.
InputStream inputStream = response.getEntity().getContent();
You can add a localContext when executing the request to manage cookies.

How to delete the file from the FTP using Proxy in c#

I have some problem in my project. I want to delete my file from the ftp using proxy.
My code is:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FtpServerName + FtpFilePath);
request.Method = WebRequestMethods.Ftp.DeleteFile;
request.Proxy = new WebProxy(ProxyAddress);
request.Proxy.Credentials = new NetworkCredential(ProxyUserName, ProxyPassword);
request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
In this i'm getting error like:
The requested FTP Command is not supported when using http proxy
can any one please help me
Thanks in advance
from http://blogs.msdn.com/b/adarshk/archive/2004/09/13/229069.aspx:
Note on using Http Proxy on FTPWebRequest: Http proxy is only supported for limited number of ftp methods (mainly to download file only), so if you have IE settings for proxy on your machine you need to explicitly set FtpWebRequest to not use proxy like below
request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
If you want to perform other FTP actions through a proxy, you'll have to find another FTP component that supports it.
Instead of request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
try request.Proxy = WebRequest.DefaultWebProxy;
Follows a demo code that worked well for me:
var request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://99.999.99.99/TextFile1.txt"));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("ftp_user", "ftp_pass"); // it's FTP credentials, not proxy
request.Proxy = WebRequest.DefaultWebProxy;
var sourceStream = new StreamReader("TextFile1.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
var response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();

Resources