Tag: http

java中的HTTP多部分类

我正试着按照这个问题的答案 但似乎默认的Android包中不包含类,因为该代码: File file = new File(“FileToSend.txt”); HttpClient client = new HttpClient(); String url = “http://www.yourdomain.com/destination.php”; PostMethod postMethod = new PostMethod(url); Part[] parts = {new FilePart(file.getName(), file)}; postMethod.setParameter(“name”, “value”); // set parameters like this instead in separate call postMethod.setRequestEntity( new MultipartRequestEntity(parts, postMethod.getParams())); int status = client.executeMethod(postMethod); 我有以下错误: Cannot instantiate the type HttpClient FilePart cannot be […]

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 HttpURLConnection返回JSON

我正在尝试发出一个返回json响应的http get请求。 我需要将json响应中的一些值存储在我的会话中。 我有这个: public String getSessionKey(){ BufferedReader rd = null; StringBuilder sb = null; String line = null; try { URL url = new URL(//url here); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(“GET”); connection.connect(); rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line + ‘\n’); } return […]

如何在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){ } } 注意:服务器端代码是 […]

创建JSON对象并将其转换为Java中的String

我需要通过httppost发送一个相当长的JSON头。 在Python中是这样的: self.body_header = { “client”: self.client_name, “clientRevision”: self.client_version, “uuid”: str(uuid.uuid4()), “session”: self.get_sessionid()} self.body = { “header”: self.body_header, “country”: {“IPR”:”1021″, “ID”:”223″, “CC1″:”0”, “CC2″:”0”, “CC3″:”0”, “CC4″:”2147483648”}, “privacy”: 1} 我需要在Java中做类似的事情,即以某种方式创建一个JSON结构,将其转换为String并通过http发送它。 问题是,我怎样才能轻松实现这一目标? 有用的库吗? 我知道如何发送它,但不知道如何构建它然后创建一个String。 谢谢你们。

如何知道HTTP服务器何时完成发送数据

我正在开发一个面向浏览器/代理的项目,我需要下载网页。 在向Web服务器发送自定义HTTP请求后,我开始侦听服务器响应。 在阅读响应时,我检查Content-Length的响应头:-row。 如果我得到其中一个,很容易确定服务器何时完成发送数据,因为我总是知道我收到了多少字节的数据。 当服务器不包含Content-Length标头并且还为进一步的请求保持连接打开时,会发生此问题。 例如,Google服务器以gzip-content响应,但不包含内容长度。 我怎么知道何时停止等待更多数据并关闭连接? 我已经考虑过在没有收到数据的情况下使用超时值来关闭连接,但这似乎是错误的方法。 例如,Chrome可以下载与我相同的页面,并且似乎总是知道何时关闭连接。

Jersey SSE – eventOutput.write在发送第一条消息后抛出nullpointer

我使用Jersey实现了一个Restful Web界面,用于通过HTTP将从内部JMS发布者收到的消息发送到外部客户端。 我已经设法将测试消息发送到Java客户端,但线程在完成write()执行,关闭连接并阻止进一步通信之前抛出空指针exception。 这是我的资源类: @GET @Path(“/stream_data”) @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput getServerSentEvents(@Context ServletContext context){ final EventOutput eventOutput = new EventOutput(); new Thread( new ObserverThread(eventOutput, (MService) context.getAttribute(“instance”)) ).start(); return eventOutput; } 这是我的线程的运行方法: public class ObserverThread implements Observer, Runnable { //constructor sets eventOutput & mService objects //mService notifyObservers() called when JMS message received //text added to Thread’s message queue […]

如何告诉java使用特定的传出ip接口来获取http请求?

有没有人知道在java中强制传出的http请求通过特定(逻辑)ip地址的快速方法? 我正在考虑使用Apache HTTP客户端(http组件的一部分),这肯定有足够的方法来实现它,但API看起来并不简单。 有没有人用它做过类似的事情? 谢谢。

使用Java套接字获取GET请求

我正在编写一个简单的程序来向特定url“ http://badunetworks.com/about/ ”发送获取请求。 如果我将其发送到“ http://badunetworks.com ”,请求仍然有效,但我需要将其发送到about页面。 package badunetworks; import java.io.*; import java.net.*; public class GetRequest { public static void main(String[] args) throws Exception { GetRequest getReq = new GetRequest(); //Runs SendReq passing in the url and port from the command line getReq.SendReq(“www.badunetworks.com/about/”, 80); } public void SendReq(String url, int port) throws Exception { //Instantiate a […]

CookieHandler困惑不解

我需要访问一些网页并通过浏览器的方式传递cookie。 这很容易使用 CookieHandler.setDefault(new MyCookieManager()); 但这引入了我需要避免的全局状态(想象一下同时访问同一服务器上的两个帐户)。 所以我想做的就像是 String doGetWithCookies(URL url, MyCookies myCookies) { HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); myCookies.addToRequest(…); myCookies.updateFromResponse(…); return getHttpBody(conn); } 但我看不出怎么做。 CookieManager.get和put方法接受一个URL ,但我想使用 具有不同URL的相同cookie 不同帐户的相同URL不同Cookie 我尝试了什么:没有什么,因为只有四种方法可用而且只有一个子类,没有任何东西适合。 手动解析标题肯定是可行的,但IMHO在2014年没有选项。我知道Apache Http客户端 ,但是1.我希望一些微不足道的东西不需要半兆字节的库,2。乍一看我看不到那里有一个解决方案。 澄清: 想象一下,你想以两个不同的用户锁定SO。 您可以在一台计算机上使用两台计算机或两台不同的浏览器(Chrome和Firefox)来完成此操作。 您无法在单个浏览器的两个选项卡中执行此操作。 我想要的是相当于模拟两个浏览器的可能性。 与此同时,我发现了一个相关问题并发布了hacky解决方案 。 我仍在寻找CookieHandler设计背后的解释。