Tag: post

Http客户端在java中发布xml文件

我需要将xml文件发送到以下链接 http://14.140.66.142:80/MSMQ/private$/votes 这是我的代码。 URL url = new URL(“http://14.140.66.142:80/MSMQ/private$/votes”); URLConnection con = url.openConnection(); String document = “C:\\Documents and Settings\\Nagra\\My Documents\\Responseserver\\workingVoting\\VoteSubmitter\\Body.xml”; FileReader fr = new FileReader(document); // specify that we will send output and accept input con.setDoInput(true); con.setDoOutput(true); char[] buffer = new char[1024*10]; int b_read = 0; if ((b_read = fr.read(buffer)) != -1) { con.setRequestHeader ( “Content-Type”, […]

如何在java中的http post中发送json对象

我想发送一个JSON对象(注意它不应该转换为字符串,因为服务器端代码基于Spring启动项目并且有params(@RequestBody PCAP pcap))我有我的下面的代码但它转换为正文到一个字符串,给我400个不良请求。 private void sendData(String ip){ try{ JSONObject json=new JSONObject(); json.put(“time_range”, “22-23”); json.put(“flow_id”, “786”); json.put(“ip_a”, “192.65.78.22”); json.put(“port_a”, “8080”); json.put(“regex”, “%ab”); URL url=new URL(“http://”+ip+”:8080/pcap”); HttpURLConnection httpcon=(HttpURLConnection)url.openConnection(); httpcon.setDoOutput(true); httpcon.setRequestMethod(“POST”); httpcon.setRequestProperty(“Accept”, “application/json”); httpcon.setRequestProperty(“Content-Type”, “application/json”); Cookie cookie=new Cookie(“user”, “abc”); cookie.setValue(“store”); httpcon.setRequestProperty(“Accept”, “application/json”); httpcon.setRequestProperty(“Cookie”, cookie.getValue()); OutputStreamWriter output=new OutputStreamWriter(httpcon.getOutputStream()); System.out.println(json); output.write(json.toString()); httpcon.connect(); String output1=httpcon.getResponseMessage(); System.out.println(output1); }catch(Exception e){ } } 注意:服务器端代码是 […]

下载由java webservice(POST)流式传输的pdf

我有一个由POST方法调用的web服务: @Path(“/ficheAction/”) @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces(“application/pdf”) public Response ficheAction(final ParamSyntheseFicheActionDto paramSyntheseFicheAction) throws EngineException { […] return Response.ok(output.toByteArray(), “application/pdf”) .header(“content-disposition”, “attachment; filename = ” + name + “.pdf”).build(); } 这个web服务使用我的js中的参数(Json): […] $.ajax({ url : webServiceUrl, // type : “POST”, // Content type to send to the server contentType : “application/json; charset=utf-8”, data : pData, dataType […]

如何使用REST将二进制文件从JQuery Client发布到Java Server

我正试图从我的客户端(jQuery)发布一个二进制文件到我的服务器(Java)。 我正在使用Apache CXF和REST。 该文件正在向服务器发送,该服务器会立即抛出exception。 这是客户端的JavaScript: function handleFileUpload() { console.log(“handleFileUpload called”); var url = “http://myserver:8181/bootstrap/rest/upload/license”; var file = $(‘#file_upload’).get(0).files[0]; $.ajax({ url: url, type: “post”, data: file, processData: false, success: function(){ $(“#file_upload_result”).html(‘submitted successfully’); }, error:function(){ $(“#file_upload_result”).html(‘there was an error while submitting’); } }); } 这是服务器端代码: @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.TEXT_PLAIN) @Path(“/license”) public String uploadLicenseFile(@FormParam(“file”) InputStream pdfStream) { try { […]

如何监控文件上传进度?

我需要将文件上传到服务器并监视它的进度。 我需要通知每次发送多少字节。 例如,在下载的情况下,我有: HttpURLConnection connection = (HttpURLConnection) m_url.openConnection(); connection.connect(); InputStream stream = connection.getInputStream(); while ((currentBytes = stream.read(byteBuffer)) > 0) { totalBytes+=currentBytes; //calculate something… } 现在我需要为上传做同样的事情。 但如果使用 OutputStreamWriter stream = new OutputStreamWriter(connection.getOutputStream()); stream.write(str); stream.flush(); 我得不到任何进程通知,关于发送了多少字节(看起来像自动操作)。 有什么建议么? 谢谢

如何在服务器端将HTTP POST请求主体作为Java String?

HttpExchange对象的getRequestBody方法返回一个InputStream。 正确阅读“身体”还有很多工作要做。 它是一个Java库+对象+方法又领先一步并将身体(在服务器端)作为即用型Java字符串返回? 谢谢!

通过java中的POST方法发送Xml字符串

我想通过POST方法将xml字符串传递给URL。 我试过下面的代码片段,但它没有返回任何内容 disableCertificateValidation(); String url = “https://..url”; //https Properties sysProps = System.getProperties(); sysProps.put(“proxySet”, “true”); sysProps.put(“proxyHost”, “1.2.3.4”); sysProps.put(“proxyPort”, “80”); Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return (new PasswordAuthentication(“userid”, “password”.toCharArray())); } }; Authenticator.setDefault(authenticator); String xml = —xml string; URL urll; HttpURLConnection connection = null; try { // Create connection urll = new URL(url); […]

Jersey UniformInterfaceException尝试代理REST POST服务

当我尝试执行以这种方式构造的代码时,我一直收到406 HTTP响应。 我已经尝试过多次重构代码和输入,但是我仍然收到这个错误,而且我已经达到了我甚至不知道要调试什么的程度。 exception似乎表明post()方法没有以所需的格式提供@FormParam ,但是你可以看到.accept(MediaType.APPLICATION_FORM_URLENCODED)和@Consumes(MediaType.APPLICATION_FORM_URLENCODED)确实匹配了。 我正在使用Firefox附加的HTTPRequester来传递@FormParam ,并确保我使用适当的Content-Type( application/x-www-form-urlencoded )传递它们。 我已经没有东西要检查了。 有没有人有任何想法? 代理服务 Client client = Client.create(); WebResource service = client.resource(myURL); Form form = new Form(); form.add(“value1”, value1); form.add(“value2”, value2); form.add(“valueN”, valueN); String returnValue = service.accept(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form); 实际服务 @POST @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Path(“/theService”) public String theService( @FormParam(“value1”) String value1, @FormParam(“value2”) String value2, @FormParam(“valueN”) String valueN) { String […]

使用Java https错误上传到Imgur v3

我目前正在尝试使用他们当前的API v3上传到imgur,但是我不断收到错误 错误:javax.net.ssl.SSLException:证书中的主机名不匹配:api.imgur.com!= imgur.com或imgur.com 这个错误是非常自我解释的,所以我想我会尝试使用http而不是我得到错误代码400与imgur。 我不确定这是否意味着我尝试上传是错误的,还是Imgur不喜欢SSL连接。 下面是我连接到Imgur的代码模块: public String Imgur (String imageDir, String clientID) { //create needed strings String address = “https://api.imgur.com/3/image”; //Create HTTPClient and post HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(address); //create base64 image BufferedImage image = null; File file = new File(imageDir); try { //read image image = ImageIO.read(file); […]

如何将标头或参数添加到使用Selenium Webdriver处理的HTTP请求中?

我正在使用Selenium Webdriver进行Web应用程序的unit testing。 它在JUnit测试中使用尽管广泛阅读可用文档并搜索,我找不到一种方法: 将标头添加到驱动程序传递的HTTP请求中。 向这样的请求添加参数,就好像驱动程序在提交表单后获得目标URL一样。 可以创建一个具有适当forms的测试网页,并让Webdriver反弹以自动获取这些参数,但这是一个非常难看的黑客。 我想避免它,特别是为了测试primefaces性。 (这是unit testing。) 在Wendriver之前,我使用Spring的MockHttpServletRequest和MockHttpServletResponse来做这个,它就像一个魅力,但我想利用Webdriver的强大function来断言目标页面的内容。