我无法正确设置user-agenthttps连接的属性.根据我收集的内容,可以通过-Dhttp.agentVM选项或通过设置http-header属性URLConnection.setRequestProperty().但是,通过VM选项设置user-agent会导致"Java/[version]"附加到http.agent的值.同时setRequestProperty()只适用于http连接,而不适用于https(至少在我尝试时).
java.net.URL url = new java.net.URL( "https://www.google.com" );
java.net.URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
conn.connect();
java.io.BufferedReader serverResponse = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
System.out.println(serverResponse.readLine());
serverResponse.close();
Run Code Online (Sandbox Code Playgroud)
我通过使用WireShark检查http通信找到/验证了问题.有没有办法解决?
更新:添加信息
似乎我对通信的看法不够深入.代码从代理后面运行,因此观察到的通信是针对代理,设置通过-Dhttps.proxyHost,而不是目标网站(google.com).无论如何,在https连接期间,方法CONNECT不是GET.这是https通信尝试的wireshark捕获.就像我上面提到的,用户代理是通过设置的,-Dhttp.agent因为URLConnection.setRequestProperty()没有效果(user-agent = Java/1.7.0).在这种情况下,请注意附加的Java/1.7.0.问题仍然存在,为什么会发生这种情况,我该如何解决这个问题呢?
CONNECT www.google.com:443 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 Java/1.7.0
Host: www.google.com
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Proxy-Connection: keep-alive
HTTP/1.1 403 Forbidden …Run Code Online (Sandbox Code Playgroud) 我正在使用 Newtonsoft 更改 web api json 输出上的属性名称。
public class User : IEntity
{
[Newtonsoft.Json.JsonProperty(PropertyName = "user_name"]
public string Username { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已启用 odata 查询,以便我可以向请求添加查询。
[HttpGet]
[Route("api/users")]
[EnableQuery]
public IQueryable<User> GetUser()
{
return dbContext.DbSet<User>();
}
Run Code Online (Sandbox Code Playgroud)
当我使用备用属性名称进行查询时,它失败了。
GET /api/users?$select=user_name
URI 中指定的查询无效。找不到名为“user_name”的属性
如果我使用实体模型名称Username(对公众不可见),则查询工作正常。如何在仍然使用 Newtonsoft 处理反序列化的同时解决这个问题?
我想要一个用户输入时间,所以就像12点,但我需要弄清楚一些事情而且我很遗憾.