• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

web service client call issue

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to web services technology and I am having some issue with calling a web service using the URL. Please help. I am getting the 405: Method not allowed error. I am sending a multi-part request to the webservice. The first part is a XML and other part is file.

import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.File;
import java.io.FileInputStream;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class UploadAttachment {
ublic static void main( String[] args ) throws Exception {

String strRSSinuityWebServiceBase = "http://host:port/MyService/";
String strServiceName = "attachments";
final String urlString = strRSSinuityWebServiceBase + strServiceName ;
final String userName = "TESTUSER";
final String password = "TESTUSE";
String lineEnd = "\r\n";

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;

java.io.File sourceFile = new java.io.File(sourcefile);
bufferSize = (int) sourceFile.lRSSth();



// open url connection
URL url = new URL( urlString );
HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);

byte[] encodedPassword = ( userName + ":" + password ).getBytes();
String encoded = DatatypeConverter.printBase64Binary(encodedPassword);
String strBoundry = "bJYQE";
con.setRequestProperty( "Authorization", "Basic " + encoded );

con.setRequestProperty("Content-Type","multipart/form-data;boundary="+strBoundry);

StringBuffer strBuffer1 = new StringBuffer();
strBuffer1.append("--"+strBoundry);
strBuffer1.append(lineEnd);
strBuffer1.append("Content-Disposition: form-data; name=\"xml\"");
strBuffer1.append(lineEnd);
strBuffer1.append("Content-Type: text/xml");
strBuffer1.append(lineEnd);
strBuffer1.append("Content-Transfer-Encoding: binary");
strBuffer1.append(lineEnd);
strBuffer1.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
strBuffer1.append(lineEnd);
strBuffer1.append("<RSS:Attachment xmlns:RSS=\"http://www.server.com/ServiceWebInterface_1.2\">");
strBuffer1.append(lineEnd);
strBuffer1.append("<RSS:DOcNumber>TRM010357</RSS:DOcNumber>");
strBuffer1.append(lineEnd);
strBuffer1.append("<RSS:DOCType>Stability Report</RSS:DOCType>");
strBuffer1.append(lineEnd);
strBuffer1.append("<RSS:Comments>Auto Uploaded file</RSS:Comments>");
strBuffer1.append(lineEnd);
strBuffer1.append("</RSS:Attachment>");
strBuffer1.append(lineEnd);
strBuffer1.append("--"+strBoundry);
strBuffer1.append(lineEnd);
strBuffer1.append("Content-Disposition: form-data; name=\"content\"; filename=\"ABC.pdf\"");
strBuffer1.append(lineEnd);
strBuffer1.append("Content-Type: application/octet-stream");
strBuffer1.append(lineEnd);
strBuffer1.append("Content-Transfer-Encoding: binary");
strBuffer1.append(lineEnd);

String strContent = strBuffer1.toString();

int fileLenth = strContent.lRSSth();

fileLenth = fileLenth+bufferSize;

con.setRequestProperty( "Content-Length", fileLenth+"");


OutputStream reqStream = con.getOutputStream();

reqStream.write(strContent.getBytes());

System.out.println("Inside the method strContent ");


FileInputStream fileInputStream = new FileInputStream(sourceFile);
bytesAvailable = fileInputStream.available();

buffer = new byte[bufferSize];

bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
reqStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
System.out.println("Inside the method strContent 2 "+con.getResponseMessage());

System.out.println(con.getResponseCode() + ":" + con.getResponseMessage()) ;

con.disconnect();
}
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic