I am writing a post transaction for a less than ideally documented device. I can properly transmit post data via 'curl' so I know the interface works. However, when I try to post the same data via HttpURLConnect I get back a 415 (Unsupported media type) error. Here is the code segment I am using:
The 2 debug println commands display the proper post data but beyond that I have no clue what is being sent or where the problem is. I have found several references to debugging this this by setting:
The problem is that I don't know where this goes. I tried adding it to the above code (line 2) but Eclipse tells me:
Multiple markers at this line
- ALL cannot be resolved to a variable
- level cannot be resolved or is not a field
- Access restriction: The type HttpURLConnection is not accessible due to restriction on required library C:\Program Files\Java
I'm guessing I am missing an import or maybe a library. Unfortunately the articles don't say what to import or where and Eclipse has no suggestions either.
Finally, it is not clear if this will really give me what I need to debug this. The article seems to imply that this will give me just the headers and not the full data transaction. I am looking for something that I can compare to the working 'curl -v' command. Can someone point me in the right direction? TIA.
This error usually indicates that the MIME type specified in the Content-Type header is not supported or is incorrect, or the HTTP header is not present.
Try using a network packet sniffer/analyzer such as Wireshark and compare what is going over the wire with your application and cURL.
If your client or server is running on Linux, you can also use tcpdump and direct the output to strings to take a quick and dirty look at your network traffic (although you will see some extraneous character displayed as data from layers below HTTP is rendered).
Dennis Putnam wrote:.. I was hoping to use Java to debug it. I guess I will need to install additional software (Wireshark).
When I have issues with networked applications, I usually start by looking at the network layers and work my way up.
Also, you might want to try removing the charset=UTF-8 parameter from the Content-Type header and see if that changes anything. I've seen where where server applications don't properly recognise the content type when header value includes parameters.
posted 4 years ago
Well, I've managed to capture what I need but I don't really know what is required and what is not so I can't tell where the problem is. This is the packet dump for 'curl' which works:
I couldn't figure out how to copy the reassembled TCP to the clipboard in Wireshark so I had to attach it as an image. As you can see there are definately differences but I don't know why or if it matters and where it does matter, I don't see how HttpURLConnection gives me any choices to change it. The order is obviously different and the host parameter is missing. Also 'Accept' is generic (*/*) for 'curl' but specific for Java. Does any of this help figure out what I'm doing wrong?