Tag: httpurlconnection

将Curl转换为Java等效项

我第一次使用New Relic REST API ,我有一个curl命令: curl -X GET ‘https://api.newrelic.com/v2/applications/appid/metrics/data.json’ \ -H ‘X-Api-Key:myApiKey’ -i \ -d ‘names[]=EndUser/WebTransaction/WebTransaction/JSP/index.jsp’ 我想在java servlet中发送此命令并从响应中获取JSON对象以备解析,什么是最佳解决方案? HttpURLConnection的? Apache httpclient? 我已经尝试了一些不同的解决方案,但到目前为止没有任何工作,我能找到的大多数示例都使用了折旧的DefaultHttpClient 以下是我尝试之一的示例: String url = “https://api.newrelic.com/v2/applications.json”; HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty(“Content-Type”, “application/json”); conn.setRequestProperty(“X-Api-Key”, “myApiKey”); conn.setRequestMethod(“GET”); JSONObject names =new JSONObject(); try { names.put(“names[]=”, “EndUser/WebTransaction/WebTransaction/JSP/index.jsp”); } catch (JSONException e) { e.printStackTrace(); } OutputStreamWriter wr= […]

-POST Json with HttpUrlConnection

当我使用python-script向此服务器发送请求时,我为我的应用程序构建了服务器端,它的工作正常,但是使用我的应用程序它不起作用。 服务器: 服务器包含一个数据库。 当我发送一个JSON {“name”:”your_name”} ,服务器在DB中保存名称。 应用(Android): @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonSend=(Button)findViewById(R.id.button_send); buttonSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AsyncTaskRunner postReq = new AsyncTaskRunner(); postReq.execute(“start”); } }); } private class AsyncTaskRunner extends AsyncTask{ @Override protected String doInBackground(String… params) { try { String url=”my url”; URL object=new URL(url); HttpURLConnection con […]

使用HttpURLConnection到PHP的POST文件

我已经引用了这个链接使用POST和HttpURLConnection发送文件 使用以下代码,我正在尝试将文件POST到本地PHP服务器。 它总是在我的PHP文件中返回文件大小0 public class FileUpload2 { String CRLF = “\r\n”; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { new FileUpload2().put(“http://localhost/test/test.php”); } public void put(String targetURL) throws Exception { String BOUNDRY = “==================================”; HttpURLConnection conn = null; try { // These strings are sent in the request […]

doInBackground不更新变量

我正在开发一个基本的Android应用程序,它使用HttpURLConnection进行POST 。 我想从Web API返回响应消息。 我的MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView mTextView = findViewById(R.id.textView); AsyncExample asyncExample = new AsyncExample(); asyncExample.execute(); mTextView.setText(asyncExample.getResponseMsg()); } } 我的AsyncExample.java class AsyncExample extends AsyncTask { private HttpURLConnection con; private String responseMsg; protected void onPreExecute() { responseMsg = “empty message”; } @Override […]

HttpURLConnection即使在setDoOutput(true)之后执行get请求,setRequestMethod(“POST”)setRequestProperty(“Content”

这是代码: String Surl = “http://mysite.com/somefile”; String charset = “UTF-8”; query = String.format(“param1=%s&param2=%s”, URLEncoder.encode(“param1”, charset), URLEncoder.encode(“param2”, charset)); HttpURLConnection urlConnection = (HttpURLConnection) new URL(Surl + “?” + query).openConnection(); urlConnection.setRequestMethod(“POST”); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setAllowUserInteraction(false); urlConnection.setRequestProperty(“Accept-Charset”, charset); urlConnection.setRequestProperty(“User-Agent”,”Android”); urlConnection.setRequestProperty(“Content-Type”,”application/x-www-form-urlencoded;charset=” + charset); urlConnection.connect(); 以上仍然是GET请求。 我在服务器上使用PHP,并且能够通过$_GET变量而不是$_POST变量访问查询的’name = value’参数 测试2.3.7(设备)。 我错过了什么?

HttpURLConnection getInputStream()有一秒延迟

我正在使用HttpURLConnection来发出POST请求。 我在测试期间始终观察到相同的行为: 第一个请求运行速度非常快(毫秒) 以下所有请求需要一秒钟+几毫秒 所以有些事情导致1秒的延迟。 它能是什么? 延迟恰好发生在HttpURLConnection#getInputStream()中。 我用HttpClient替换了实现 – 然后一切正常,没有第二次延迟(所以它不是服务器故障)。 但我真的不想使用任何外部依赖,所以我想修复HttpURLConnection的事情……任何想法? 目前的实施情况。 我尝试了stackoverflow的一些提示(向请求添加标题),但没有成功。 URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod(“POST”); con.setRequestProperty(“Content-Length”, “” + (body == null ? 0 : body.length)); // Send post request con.setDoOutput(true); OutputStream wr = con.getOutputStream(); if (body != null) { wr.write(body); } wr.flush(); wr.close(); BufferedReader rd = […]

如何获取文件属性而不下载

如果我有文件的URL,如何在不下载文件的情况下获取文件属性(例如日期)。 我必须实现更新检查器。 为此,我将比较两个文件的日期。 一个是本地的,第二个是相同的文件,但在服务器上有最新的日期。 我知道第二个文件的URL。 请给我一个建议 – 如何在不下载的情况下查看第二个文件的日期? 我想过通过hash进行比较,但我需要检查服务器上的文件是否是最新版本?

用于POST请求的HttpURLConnection App Engine Java示例不起作用

我正在尝试在app引擎应用程序下使用urlfetch执行POST请求。 我已按照“使用HttpURLConnection”部分下的App Engine文档(此处为https://developers.google.com/appengine/docs/java/urlfetch/usingjavanet )中的简单示例提取的说明(和代码)进行了操作。 。 import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStreamWriter; String message = URLEncoder.encode(“my message”, “UTF-8”); try { URL url = new URL(“http://httpbin.org/post”); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod(“POST”); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(“message=” + message); writer.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { […]

“java.lang.IllegalStateException:在建立连接后无法设置请求属性”在Android中出错

我是Android的新手,试图获取网页的HTML内容,这就是我遇到这个问题的时候。 java.lang.IllegalStateException: Cannot set request property after connection is made 代码: public static String getHTMLPage(String inputURL, String host, String referer, String cookie, String breakString) { String htmlContent = “”; try { URL u = new URL(inputURL); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); uc.setRequestProperty(“Host”, host); uc.setRequestProperty(“Connection”, “keep-alive”); if (!referer.isEmpty()) { uc.setRequestProperty(“Referer”, referer); } uc.setRequestProperty(“User-Agent”, “Mozilla/5.0 (Windows NT […]

如何防止来自HttpsURLConnection的CONNECT方法调用

我有一个Android客户端向服务器发出HTTPS请求。 防火墙日志包含不需要的CONNECT请求方法的条目。 什么时候会发送CONNECT请求,如何防止它被发送? 我希望只有GET请求。 我的理解是,对openConnection()的调用实际上并没有发出请求,并且GET请求将继续调用getResponseMessage()。 如何禁用http客户端尝试建立代理隧道? 以下是我发送连接并拨打电话的方式: URL url = new URL(“https://some.server.com”); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setReadTimeout(REAT_TIMEOUT); connection.setRequestProperty(ACCEPT_CHARSET, UTF8_CHARSET); connection.setRequestProperty(CONTENT_TYPE_HEADER, contentType); setCustomRequestProperties(connection); connection.setRequestMethod(“GET”); //create response connection.getResponseMessage(); connection.getResponseCode(); connection.getContentType(); connection.getContent(); 编辑: 这是我要阻止的防火墙日志条目: CONNECT someURL.domain.com:443 HTTP/1.1 Host: someURL.domain.com User-Agent: CustomUserAgent;1.0.0(Android 4.3;Nexus 4;T-Mobile) Proxy-Connection: Keep-Alive X-SSL-Secure: true X-CS-Source-IP: 10.3.3.3 X-Forwarded-For: 10.3.3.3 我认为这是代理相关的,因为“代理连接”标题