using URL.openConnection

I need to set the contentType on the URLConnection.
I am opening a connection to a servlet, writing a binary to that open stream and closing it. This binary could be an powerpoint, word document or pdf file.
I need to be able to set the request type how can this be done as there is no setContentType method on the class URLConnection.

Thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use the setRequestProperty:

urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
use these content types

inzaghiAuthor Commented:
I cannot seem to invoke the doPost method of the servlet. the doGet works fine.If i Use the following code

URL obj = new URL(url);
URLConnection  connection = obj.openConnection();
connection.setRequestProperty("Content-Type", type);
// write to stream
// flush stream
///close stream

I have placed some System.out in the doPost but it is not gettign invoked. Any idea?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

>Any idea?  
Did you try adding a setDoInput and  receiving some kind of response from the servlet(maybe just a String like "thanks I got it").
when you call a servlet from a browser, the browser will take care of ur HTTP Request
Since you are calling a servlet from your program, you should write the HTTP request by yourself.

thats why doPost is not getting invoked.,
setDoInput is by default true but as rrz@871311 said, its important to get connection's InputStream and even if you dont want to read anything, it must be closed

inzaghi why exactly didn't I get some points? The link I posted contains *exactly* the information provided by other experts.
I object. I thought  hoomanv and myself were helpful in pointing out the problem(in my opinion it was the lack of accepting a response from the server).
What does inzaghi say the solved his problem ?
Thank you GhostMod I appreciate it :)
inzaghiAuthor Commented:
Sorry guys for not distributing points appropiateley
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.