如果您对使用Node.js的HTTPPUT请求和node.jspost请求感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解使用Node.js的HTTPPUT请求的各种细节,并对node.jspo
如果您对使用Node.js的HTTP PUT请求和node.js post请求感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解使用Node.js的HTTP PUT请求的各种细节,并对node.js post请求进行深入的分析,此外还有关于c# http put请求 通用http请求 post get put、java – 使用http-header“接受”的HttpURLConnection GET请求、Node.js Express中的HTTP GET请求、node.js – NodeJS中带有查询字符串的HTTPS请求的实用技巧。
本文目录一览:- 使用Node.js的HTTP PUT请求(node.js post请求)
- c# http put请求 通用http请求 post get put
- java – 使用http-header“接受”的HttpURLConnection GET请求
- Node.js Express中的HTTP GET请求
- node.js – NodeJS中带有查询字符串的HTTPS请求
使用Node.js的HTTP PUT请求(node.js post请求)
我试图弄清楚如何使用node.js发出HTTP PUT请求。我尝试了很多不同的方法,但是无法正常工作。
这个想法是要有一种放置文件的方法,例如:
function putFile(file_path,callback) {
// Put the file
}
任何帮助,将不胜感激。
c# http put请求 通用http请求 post get put
POST 需要把 contentType 改为 application/x-www-form-urlencoded; charset=UTF-8
PUT 需要把 contentType 改为application/json
/// <summary>
/// 通用请求方法
/// </summary>
/// <param name="url"></param>
/// <param name="datas"></param>
/// <param name="method">POST GET PUT DELETE</param>
/// <param name="contentType">"POST application/x-www-form-urlencoded; charset=UTF-8"</param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string HttpRequest(string url, string data, string method = "PUT", string contentType = "application/json", Encoding encoding = null)
{
byte[] datas = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);//data可以直接传字节类型 byte[] data,然后这一段就可以去掉
if (encoding == null)
encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = method;
request.Timeout = 150000;
request.AllowAutoRedirect = false;
if (!string.IsNullOrEmpty(contentType))
{
request.ContentType = contentType;
}
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
}
Stream requestStream = null;
string responseStr = null;
try
{
if (datas != null)
{
request.ContentLength = datas.Length;
requestStream = request.GetRequestStream();
requestStream.Write(datas, 0, datas.Length);
requestStream.Close();
}
else
{
request.ContentLength = 0;
}
using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
{
Stream getStream = webResponse.GetResponseStream();
byte[] outBytes = ReadFully(getStream);
getStream.Close();
responseStr = Encoding.UTF8.GetString(outBytes);
}
}
catch (Exception)
{
throw;
}
finally
{
request = null;
requestStream = null;
}
return responseStr;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[512];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
java – 使用http-header“接受”的HttpURLConnection GET请求
也许这是一个愚蠢的问题,但我如何使用httpURLConnection和http-header“Accept”请求(GET)JSON响应?
我在文档中找到了一个片段,但我不知道怎么做.
Accept = "Accept" ":" #( media-range [ accept-params ] )
解决方法
如果是这样,那么你可以写
URL url = new URL("https://stackoverflow.com"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("Accept","application/json"); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ... } finally { urlConnection.disconnect(); }
Source
Node.js Express中的HTTP GET请求
如何在Node.js或Express.js中发出HTTP请求?我需要连接到另一个服务。我希望该调用是异步的,并且该回调包含远程服务器的响应。
node.js – NodeJS中带有查询字符串的HTTPS请求
看看HTTPS request in NodeJS,我有很长的路要走,但我的请求似乎没有包含查询参数……
使用上面的例子,我的mods:
var $q = require('q'),_ = require('lodash'),path = require('path'),Qs = require('qs'),uuid = require('node-uuid'),https = require('https'); var params = { schema: '1.0',form: 'json',username: 'name' } var options = { host: 'openshift.redhat.com',port: 443,path: '/broker/rest/api',query: params }; var req = https.get(options,function(res) {
解决方法
Request path. Defaults to ‘/’. Should include query string if any. E.G. ‘/index.html?page=12’.
今天关于使用Node.js的HTTP PUT请求和node.js post请求的分享就到这里,希望大家有所收获,若想了解更多关于c# http put请求 通用http请求 post get put、java – 使用http-header“接受”的HttpURLConnection GET请求、Node.js Express中的HTTP GET请求、node.js – NodeJS中带有查询字符串的HTTPS请求等相关知识,可以在本站进行查询。
本文标签: