Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我要用java post请求响应状态代码为500的内部服务器错误?_Java_Python_Http_Request_Bounding Box - Fatal编程技术网

为什么我要用java post请求响应状态代码为500的内部服务器错误?

为什么我要用java post请求响应状态代码为500的内部服务器错误?,java,python,http,request,bounding-box,Java,Python,Http,Request,Bounding Box,嘿,我是新来的 实际上,我正在尝试从官方数据库()获取数据。 我使用java并尝试通过带有参数的post请求获取信息。 我编写了两个不同的版本,一个是Apache httpclient,另一个是java.net。 我还有一个用Python编写的版本(不是我的) java中的那个不起作用!我至少得到以下错误作为响应: 状态代码:500内部服务器错误{“d”:{“消息”:“Fehler bei” 德苏切纳赫酒店 Funkanlagenstandorten.,“StackTrace”:“源”:“内部异

嘿,我是新来的

实际上,我正在尝试从官方数据库()获取数据。 我使用java并尝试通过带有参数的post请求获取信息。 我编写了两个不同的版本,一个是Apache httpclient,另一个是java.net。 我还有一个用Python编写的版本(不是我的)

java中的那个不起作用!我至少得到以下错误作为响应:

状态代码:500内部服务器错误
{“d”:{“消息”:“Fehler bei” 德苏切纳赫酒店 Funkanlagenstandorten.,“StackTrace”:“源”:“内部异常”:“}”


System.InvalidOperationException:“GetStandorteFreigabe”是kein gültiger Webdienst MethodName。 bei System.Web.Services.Protocols.HttpServerProtocol.Initialize() 创建(类型类型、HttpContext上下文、HttpRequest请求、HttpResponse响应、布尔和中止处理)

导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.Set;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.CookieStore;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.BasicCookieStore;
导入org.apache.http.impl.client.HttpClientBuilder;
导入org.json.simple.JSONObject;
导入objects.BoundingBox;
导入对象.Header;
公共类HttpClientTest
{
//边界框下的TODO Gröe?
公共静态void main(字符串[]args)
{
尝试
{
新的HttpClientTest();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
收割台;
@抑制警告(“未选中”)
public HttpClientTest()引发ClientProtocolException,IOException
{
列表框=getBboxes();
用于(边界框bBox:框)
{
HttpClient=null;
CookieStore CookieStore=新的BasicCookieStore();
HttpClientBuilder=HttpClientBuilder.create().setDefaultCookieStore(cookieStore);
client=builder.build();
字符串cookie=getCookie();
HttpPostRequest=新的HttpPost(
“https://“+Start.EMF+”.bundesnetzagentur.de/karte/Standortservice.asmx/GetStandorteFreigabe”);
header=newheader().getDefaultHeader();
添加(“Cookie”,Cookie);
Set entries=header.keySet();
for(字符串键:条目)
postRequest.addHeader(key,header.get(key));
//请求参数和其他属性。
JSONObject obj=新的JSONObject();
JSONObject subobject=新的JSONObject();
subObj.put(“被起诉”,bBox.getSouth());
subObj.put(“west”,bBox.getWest());
subObj.put(“nord”,bBox.getNorth());
subObj.put(“ost”,bBox.getEast());
对象放置(“框”,subObj);
System.out.println(“RequestJSON:+obj.toJSONString());
StringEntity requestEntity=新的StringEntity(obj.toJSONString());
setContentEncoding(“UTF-8”);
setContentType(“应用程序/json”);
postRequest.setEntity(requestEntity);
//执行并获得响应。
HttpResponse response=client.execute(postRequest);
System.out.println(response.getStatusLine());
System.out.println(Arrays.deepToString(response.getAllHeaders());
HttpEntity=response.getEntity();
如果(实体!=null)
{
StringBuilder sb=新的StringBuilder();
try(InputStream instream=entity.getContent())
{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(instream));
for(字符串行;(line=reader.readLine())!=null;)
{
sb.追加(第+行“\r\n”);
}
reader.close();
//做些有用的事
}
System.out.println(sb.toString());
}
}
}
私有字符串getCookie()
{
StringBuilder cookieStringBuilder=新StringBuilder();
尝试
{
HttpClient=null;
CookieStore CookieStore=新的BasicCookieStore();
HttpClientBuilder=HttpClientBuilder.create().setDefaultCookieStore(cookieStore);
client=builder.build();
HttpGet-HttpGet=newhttpget(“https://“+Start.EMF+”.bundesnetzagentur.de/karte/Default.aspx”);
client.execute(httpGet);
cookieStore.getCookies().forEach(e->
{
append(e.getName()+“=”+e.getValue());
cookieStringBuilder.append(“;”);
});
}
捕获(IOE1异常)
{
e1.printStackTrace();
}
System.out.println(“Cookie:+cookieStringBuilder.toString());
返回cookieStringBuilder.toString();
}
私有列表getBboxes()
{
//BoundingBox=新的BoundingBox();
BoundingBox=新的边界框(13.679351806640627、51.06313741319562、13.723812103271484、,
51.082174834773625);
List bBoxes=new ArrayList();
b框。添加(框);
//双南、西、北、东;
//south=box.getSouth();
//

//而(south您的代码似乎可以工作。我刚刚尝试过,只更改了示例中提供的坐标值,其中一些是从我的浏览器调试控制台提取的,可以工作:

{“nord”:51.7151177895987,“ost”:12.70294189453125,“sue”
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONObject;

import objects.BoundingBox;
import objects.Header;

public class HttpClientTest
{
    // TODO Größe der Bounding Box?
    public static void main(String[] args)
    {
       try
       {
          new HttpClientTest();
       }
       catch (IOException e)
       {
          e.printStackTrace();
       }
    }

    Header header;

    @SuppressWarnings("unchecked")
    public HttpClientTest() throws ClientProtocolException, IOException
    {
       List<BoundingBox> boxes = getBboxes();
       for (BoundingBox bBox : boxes)
       {
          HttpClient client = null;
          CookieStore cookieStore = new BasicCookieStore();
          HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(cookieStore);
          client = builder.build();

          String cookie = getCookie();
          HttpPost postRequest = new HttpPost(
                "https://" + Start.EMF + ".bundesnetzagentur.de/karte/Standortservice.asmx/GetStandorteFreigabe");
          header = new Header().getDefaultHeader();
          header.add("Cookie", cookie);
          Set<String> entries = header.keySet();
          for (String key : entries)
             postRequest.addHeader(key, header.get(key));

          // Request parameters and other properties.
          JSONObject obj = new JSONObject();
          JSONObject subObj = new JSONObject();
          subObj.put("sued", bBox.getSouth());
          subObj.put("west", bBox.getWest());
          subObj.put("nord", bBox.getNorth());
          subObj.put("ost", bBox.getEast());
          obj.put("Box", subObj);
          System.out.println("RequestJSON: " + obj.toJSONString());

          StringEntity requestEntity = new StringEntity(obj.toJSONString());
          requestEntity.setContentEncoding("UTF-8");
          requestEntity.setContentType("application/json");
          postRequest.setEntity(requestEntity);

          // Execute and get the response.
          HttpResponse response = client.execute(postRequest);
          System.out.println(response.getStatusLine());
          System.out.println(Arrays.deepToString(response.getAllHeaders()));

          HttpEntity entity = response.getEntity();
          if (entity != null)
          {
             StringBuilder sb = new StringBuilder();
             try (InputStream instream = entity.getContent())
             {
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                for (String line; (line = reader.readLine()) != null;)
                {
                    sb.append(line + "\r\n");
                }

                reader.close();
                // do something useful
             }
             System.out.println(sb.toString());
          }
       }

    }

    private String getCookie()
    {
       StringBuilder cookieStringBuilder = new StringBuilder();
       try
       {
          HttpClient client = null;
          CookieStore cookieStore = new BasicCookieStore();
          HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(cookieStore);
          client = builder.build();
          HttpGet httpGet = new HttpGet("https://" + Start.EMF + ".bundesnetzagentur.de/karte/Default.aspx");
          client.execute(httpGet);
          cookieStore.getCookies().forEach(e ->
             {
                cookieStringBuilder.append(e.getName() + " = " + e.getValue());
                cookieStringBuilder.append(";");
             });

       }
       catch (IOException e1)
       {
          e1.printStackTrace();
       }

       System.out.println("Cookie: " + cookieStringBuilder.toString());
       return cookieStringBuilder.toString();
    }

    private List<BoundingBox> getBboxes()
    {
//     BoundingBox box = new BoundingBox();
       BoundingBox box = new BoundingBox(13.679351806640627, 51.06313741319562, 13.723812103271484,
             51.082174834773625);
       List<BoundingBox> bBoxes = new ArrayList<>();
       bBoxes.add(box);
//     double south, west, north, east;
//     south = box.getSouth();
//
//     while (south <= box.getNorth())
//     {
//        north = south + JavaDefaultTest.STEP;
//        west = box.getWest();
//        while (west <= box.getEast())
//        {
//           east = west + JavaDefaultTest.STEP;
//           bBoxes.add(new BoundingBox(west, south, east, north));
//           west += JavaDefaultTest.STEP;
//        }
//        south += JavaDefaultTest.STEP;
//     }
       return bBoxes;
    }
}
package objects;

import java.util.ArrayList;
import java.util.List;

/**
 * order: west, south, east, north
 * 
 * @author Zinke
 *
 */
public class BoundingBox
{
    public final static double STEP = 0.03d;
    private double north;
    private double east;
    private double west;
    private double south;

    private static final double northMin = 47.0f, northMax = 55.0f;
    private static final double eastMin = 5.0f, eastMax = 16.0f;

    /**
     * <b>Initial constructor</b><br>
     * <br>
     * Bounding box for data scraping: Germany<br>
     * coordiantes in decimal degree<br>
     * <br>
     * order: west, south, east, north
     */
    public BoundingBox()
    {
       setWest(eastMin);
       setSouth(northMin);
       setEast(eastMin + STEP);
       setNorth(northMin + STEP);
    }

    /**
     * Bounding box for data scraping: Germany<br>
     * coordiantes in decimal degree<br>
     * order: west, south, east, north
     * 
     * @param west
     * @param south
     * @param east
     * @param north
     */
    public BoundingBox(double north, double east,double south,double west)
    {
       setWest(west);
       setNorth(north);
       setEast(east);
       setSouth(south);
    }

    public double getNorth()
    {
       return north;
    }

    public void setNorth(double north)
    {
       this.north = north;
    }

    public double getEast()
    {
       return east;
    }

    public void setEast(double east)
    {
       this.east = east;
    }

    public double getWest()
    {
       return west;
    }

    public void setWest(double west)
    {
       this.west = west;
    }

    public double getSouth()
    {
       return south;
    }

    public void setSouth(double south)
    {
       this.south = south;
    }

    /**
     * Returns a list with bounding boxes for whole germany <br>
     * From Southest+Westest Point beginning
     * 
     * @return
     */
    public static List<BoundingBox> getAllBboxes()
    {
       BoundingBox box = new BoundingBox();
       List<BoundingBox> bBoxes = new ArrayList<>();
       bBoxes.add(box);// set Starting Box

       double south, west, north, east;
       south = box.getSouth();
       while (south <= northMax)
       {
          north = south + BoundingBox.STEP;
          west = box.getWest();
          while (west <= eastMax)
          {
             east = west + BoundingBox.STEP;
             bBoxes.add(new BoundingBox(north,east,south,west));
             west += BoundingBox.STEP;
          }
          south += BoundingBox.STEP;
       }
       return bBoxes;
    }
}