Tag: httpurlconnection

无法重置方法:已连接

我已经看到如果之前调用getInputStream会抛出此错误,但我没有在我的代码中并且我已检查connected属性为false,但仍显示此错误。 代码如下: private void testConnect() throws Exception{ HttpURLConnection con = null; OutputStream httpsend = null; String url = “https://xxxxx/goldapi/rs/checkPaymentService”; TrustManager[] trustall = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {} public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {} } }; SSLContext sc […]

如何在httpurlconnection android中添加参数

下面的代码成功将图像发送到服务器。 但我需要的是必须向数据库添加图像描述和图像日期等参数。 我不知道如何在HttURLconnection中添加参数。 String fileName = sourceFileUri; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = “\r\n”; String twoHyphens = “–“; String boundary = “*****”; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File sourceFile = new File(sourceFileUri); FileInputStream fileInputStream = new FileInputStream(sourceFile_imagepath); URL url = new […]

如何将来自我的Web / app服务器(java)的失败消息重新传输到FCM Server,我该怎么办?

我想发送1000条消息并仅重新发送失败的消息。 有什么办法吗? 是否有任何方法可以识别每个失败的消息,例如唯一的消息ID? 如您所见,没有可识别的消息ID。 实际上可能只有一条消息应该重新发送,就像在这种情况下一样? 我应该只重新发送此消息还是重发? 我应该使用指数退避吗? 然后,我在哪里可以找到示例源?

Android – 来自Map 的HttpURLConnection参数通过POST方法

这只是问答。 仅仅意味着一个维基百科。 我经常搜索以实现这一目标。 所有人都发布了不同类型的答案。 我现在成功使用此代码。 所以我想与所有人分享。 您的HttpURLConnection必须位于异步任务(doInBackground)中。 脚步: 在Map Function中设置参数 在表单中构建字符串: param=val&param2=val2 设置HttpUrlConnection OutputStream用于OutputStream 。 使用InputStream从服务器读取响应 设置参数: Map params = new LinkedHashMap(); params.put(“tag”, “sda”); try { StringBuilder postData = new StringBuilder(); for (Map.Entry param : params.entrySet()) { if (postData.length() != 0) postData.append(‘&’); postData.append(URLEncoder.encode(param.getKey(), “UTF-8”)); postData.append(‘=’); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), “UTF-8”)); } String postdata = postData.toString(); Log.w(“postData”, postdata); //HERE […]

jre8中URLPermission处的IllegalArgumentException

当我运行下面的代码时, – 在JRE8上的一个Applet中,在con.getInputStream()行上它抛出exception – 在JRE7或JRE6上的Applet中它不会抛出。 – 在任何JRE上的桌面应用程序中它不会抛出。 当我删除行以setRequestPropery开头时,它不会在任何JRE上抛出exception。 URLConnection con = new URL(adress).openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty(“Content-Type”, “application/octet-stream”); con.setRequestProperty(“pragma:”, “no-cache”); PrintStream ps = new PrintStream(con.getOutputStream()); ps.println(“Test”); ps.close(); in = new DataInputStream(conn.getInputStream()); 例外: java.lang.IllegalArgumentException: invalid actions string at java.net.URLPermission.init(Unknown Source) at java.net.URLPermission.(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.URLtoSocketPermission(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source) 在我的applet中,我试图打开一个连接,我需要这些请求属性。 你知道JRE8上导致这个exception的原因吗? 为什么只在applet而不是desktopapp。

Google App Engine(Java):URL获取响应过大的问题

我正在尝试在谷歌应用程序上构建某种web服务。 现在问题是,我需要从网站获取数据(HTML Scraping)。 请求如下: URL url = new URL(p_url); con = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(con.getInputStream()); BufferedReader reader = new BufferedReader(in); String result = “”; String line = “”; while((line = reader.readLine()) != null) { System.out.println(line); } return result; 现在,App Engine在第3行给出了以下例外情况: com.google.appengine.api.urlfetch.ResponseTooLargeException 这是因为最大请求限制为1mb,页面的总HTML大约为1.5mb。 现在我的问题是:我只需要html的前20行来刮。 有没有办法只获取HTML的一部分,以便不会抛出ResponseTooLargeException? 提前致谢!

HttpURLConnection conn.getRequestProperty返回null

我正在尝试将一些数据推送到BES的URL(MDS_CS) 当我在我的代码中设置一些请求标头并提交请求时,提交的请求的标头被设置为null 。 这是我的代码: HttpURLConnection conn =(HttpURLConnection)url.openConnection(); conn.setDoInput(true);//For receiving the confirmation conn.setDoOutput(true);//For sending the data conn.setRequestMethod(“POST”);//Post the data to the proxy conn.setRequestProperty(“X-Rim-Push-ID”, pushId); conn.setRequestProperty(“Content-Type”, “text/html”); conn.setRequestProperty(“X-Rim-Push-Title”, “-message”); conn.setRequestProperty(“X-Rim-Push-Type”, “browser-message”); conn.setRequestProperty(“X-Rim-Push-Dest-Port”, “7874”); //Write the data OutputStream out = conn.getOutputStream(); out.write(data.getBytes()); out.close(); System.out.println(conn.getHeaderField(“X-Rim-Push-ID”)); 当我尝试检索X-Rim-Push-Title时,最后一行返回null只有正确检索的X-Rim-Push-ID为NULL 请帮我

如何使用HttpURLConnection和Java中的CookieManager为每个连接使用不同的cookie

我需要使用HttpURLConnection同时从多个线程连接到一个网站,但是为每个连接使用不同的cookie。 由于Java仅支持设置全局CookieManager,因此我实现了以下hack。 我没有调用CookieHandler.setDefault(new CookieManager()) ,而是实现了一个自定义CookieHandler ,它为每个线程使用不同的CookieStore实例,在每个请求后清除它。 我已经基于CookieManager的源代码创建了名为SessionCookieManager的类。 cookieJar成员变量已被删除,其用法已被getCookieStore()取代。 添加了以下代码: public class SessionCookieManager extends CookieHandler { private final static SessionCookieManager ms_instance = new SessionCookieManager(); public static SessionCookieManager getInstance() { return ms_instance; } private final static ThreadLocal ms_cookieJars = new ThreadLocal() { @Override protected synchronized CookieStore initialValue() { return new sun.net.www.protocol.http.InMemoryCookieStore(); } }; public void clear() { […]

在URL.openconnection()时做了什么?

我想知道在URL.openconnection()时做了什么。 我做了一些像这样的测试: public static void main(String[] args) { // testConnection(“http://www.google.com”); testConnection(“http://219.09.34.23.1”); } private static void testConnection(final String _url) { new Thread(new Runnable() { String strurl = _url; long starttime = 0; long endtime = 0; public void run() { try { System.out.println(“open:” + strurl); starttime = System.currentTimeMillis(); System.out.println(“starttime:” + starttime); URL url = new URL(strurl); […]

Java – HttpUrlConnection每次都返回缓存的响应

我正在尝试收集Roblox货币兑换的统计数据进行分析。 因此,我需要最新的数据而不是缓存的结果。 但是,似乎无论我做什么,结果仍然是缓存的。 似乎最直观的选项setUseCaches()没有效果,并且手动将头设置为Cache-Control: no-cache似乎也不起作用。 我使用Fiddler2检查了Cache头,看到它的值是Cache-Control: max-age=0 ,但它似乎也没有改变程序的行为。 以下是相关的代码: url: private final static String URL = “http://www.roblox.com/my/money.aspx#/#TradeCurrency_tab”; GET请求: URLConnection socket = new URL( URL ).openConnection( ); socket.setUseCaches( false ); socket.setDefaultUseCaches( false ); HttpURLConnection conn = ( HttpURLConnection )socket; conn.setUseCaches( false ); conn.setDefaultUseCaches( false ); conn.setRequestProperty( “Pragma”, “no-cache” ); conn.setRequestProperty( “Expires”, “0” ); conn.setRequestProperty( “Cookie”, “.ROBLOSECURITY=” […]